2013-05-28 4 views
1

나는 객체의 메소드를 호출 할 필요가 있지만 오류가 점점 계속 : 내가 웹 응용 프로그램을하고 있어요문자열을 분리하는 방법은 무엇입니까?

'str' object has no attribute 'go'

나는 사용자가 양식을 제출하면 새로운 방을로드해야합니다.

class GameEngine(object): 

    def GET(self): 
     if session.room: 
      return render.show_room(room=session.room) 
     else: 
      return render.you_died() 

    def POST(self): 
     form = web.input(action=None) 

     if session.room and form.action: 
      session.room = session.room.go(form.action) 

     web.seeother("/game") 

각 session.room에는 경로가 있으며 사용자가 양식에서 선택한 경로로 이동해야합니다. 이 버그를 어떻게 수정합니까?

새 방으로 가서 렌더링하고 싶습니다.

from random import randint 

class Room(object): 

    def __init__(self, name, description): 
     self.name = name 
     self.description = description 
     self.paths = {} 

    def go(self, direction): 
     return self.paths.get(direction, None) 

    def add_paths(self, paths): 
     self.paths.update(paths) 

central_corridor = Room("Central Corridor", 
""" 
The Gothons of Planet Percal #25 have invaded your ship and destroyed 
your entire crew. You are the last surviving member and your last 
mission is to get the neutron destruct bomb from the Weapons Armory, 
put it in the bridge, and blow the ship up after getting into an 
escape pod. 

You're running down the central corridor to the Weapons Armory when 
a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume 
flowing around his hate filled body. He's blocking the door to the 
Armory and about to pull a weapon to blast you. 
""") 


laser_weapon_armory = Room("Laser Weapon Armory", 
""" 
Lucky for you they made you learn Gothon insults in the academy. 
You tell the one Gothon joke you know: 
Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr. 
The Gothon stops, tries not to laugh, then busts out laughing and can't move. 
While he's laughing you run up and shoot him square in the head 
putting him down, then jump through the Weapon Armory door. 

You do a dive roll into the Weapon Armory, crouch and scan the room 
for more Gothons that might be hiding. It's dead quiet, too quiet. 
You stand up and run to the far side of the room and find the 
neutron bomb in its container. There's a keypad lock on the box 
and you need the code to get the bomb out. If you get the code 
wrong 10 times then the lock closes forever and you can't 
get the bomb. The code is 3 digits. 
""") 


the_bridge = Room("The Bridge", 
""" 
The container clicks open and the seal breaks, letting gas out. 
You grab the neutron bomb and run as fast as you can to the 
bridge where you must place it in the right spot. 

You burst onto the Bridge with the netron destruct bomb 
under your arm and surprise 5 Gothons who are trying to 
take control of the ship. Each of them has an even uglier 
clown costume than the last. They haven't pulled their 
weapons out yet, as they see the active bomb under your 
arm and don't want to set it off. 
""") 


escape_pod = Room("Escape Pod", 
""" 
You point your blaster at the bomb under your arm 
and the Gothons put their hands up and start to sweat. 
You inch backward to the door, open it, and then carefully 
place the bomb on the floor, pointing your blaster at it. 
You then jump back through the door, punch the close button 
and blast the lock so the Gothons can't get out. 
Now that the bomb is placed you run to the escape pod to 
get off this tin can. 

You rush through the ship desperately trying to make it to 
the escape pod before the whole ship explodes. It seems like 
hardly any Gothons are on the ship, so your run is clear of 
interference. You get to the chamber with the escape pods, and 
now need to pick one to take. Some of them could be damaged 
but you don't have time to look. There's 5 pods, which one 
do you take? 
""") 

pod = randint(1,6) 
the_end_winner = Room("The End", 
""" 
You jump into pod %r and hit the eject button. 
The pod easily slides out into space heading to 
the planet below. As it flies to the planet, you look 
back and see your ship implode then explode like a 
bright star, taking out the Gothon ship at the same 
time. You won! 
""" % pod) 


the_end_loser = Room("The End", 
""" 
You jump into a random pod and hit the eject button. 
The pod escapes out into the void of space, then 
implodes as the hull ruptures, crushing your body 
into jam jelly. 
""" 
) 
rn = randint(1,4) 

escape_pod.add_paths({ 
    '%r' % pod : the_end_winner, 
    '*': the_end_loser 
}) 

generic_death = {1: "You died. You kinda suck at this.", 
2:"Your mom would be proud...if she were smarter.", 
3:"Such a luser.", 
4:"I have a small puppy that's better at this."} 

the_bridge.add_paths({ 
'throw the bomb': generic_death[rn], 
'slowly place the bomb': escape_pod 
}) 

randcode = '%r%r' % (randint(0,9),randint(0,9)) 
laser_weapon_armory.add_paths({ 
randcode : the_bridge, 
'*': generic_death[rn] 
}) 

central_corridor.add_paths({ 
'shoot!': generic_death[rn], 
'dodge!': generic_death[rn], 
'tell a joke': laser_weapon_armory 
}) 

START = central_corridor 
+3

사용 올바른 객체, 문자열 session.room가 설정됩니다 위치를 coule 좀 더 코드를 제공하는 경우, – dm03514

+6

는 당신이 우리를 보여줄 수 매우 도움이 될 것입니다,이 경우 사용하고 싶은 될 것 같지 않습니다 그게 어떨 것 같아? – cmd

+0

app.py에는'gotonweb 가져 오기 맵에서 '맵 모듈에 Room 클래스와 이름과 설명 속성을 가진 여러 개의 방이 있습니다. 방은 다른 방을 go (다른 방 방법)를 사용하여 입력 할 수있는 경로를 제공합니다 (방 방법).첫 번째 session.room은'session.room = map.START'이며 central_corridor라고하는 방입니다. 원한다면 전체 파일을 게시 할 수 있습니다. 하지만 세션을 변경할 수 있다면 문제가 해결 될 것이라고 생각합니다. '인용문'이 없어서 메소드를 호출 할 수 있습니다. –

답변

2

내가 의심으로 : 여기

import web 
from gothonweb import map 

urls = (
'/game', 'GameEngine', 
'/', 'Index', 
) 

app = web.application(urls, globals()) 

if web.config.get('_session') is None: 
    store = web.session.DiskStore('sessions') 
    session = web.session.Session(app, store, 
           initializer={'room': None}) 
    web.config._session = session 
else: 
    session = web.config._session 

render = web.template.render('templates/', base="layout") 


class Index(object): 
    def GET(self): 
     session.room = map.START 
     web.seeother("/game") 


class GameEngine(object): 

    def GET(self): 
     if session.room: 
      return render.show_room(room=session.room) 
     else: 
      return render.you_died() 

    def POST(self): 
     form = web.input(action=None) 

     if session.room and form.action: 
      session.room = session.room.go(form.action) 
      return render.show_room(room=session.room) 

     web.seeother("/game") 

if __name__ == "__main__": 
    app.run() 

가 gothonweb/map.py 파일입니다 : 문제는 여기에 전체 app.py 파일이있는

if session.room and form.action: 
    session.room = session.room.go(form.action) 

에 위의 설명 :

몇 가지 실수를하면 발생하는 오류가 발생하거나 우연히 발생하지 않았습니다.

경로 사전에는 Room 개 개체 만 허용됩니다. - 문자열이 아닌 Room 개체입니다

  1. 모든 경로가 어딘가 generic_death[rn] 있습니다

    그러나 여러 장소에서

    , 당신은 다른 뭔가가있다. 이걸 바꿔야 해.

  2. 수행 한 작업이 존재하지 않는 경우 같은 방에 머물하기 위해

    def go(self, direction): 
        return self.paths.get(direction, self) 
    

    을 할 수 좋을 것이다.

  3. dict 중 일부는 '*' 키를 가지고 있습니다. 당신이 "다른 모든 경우에"항목을 구현한다고 가정하면, 나는 그것이 '*' 위해 노력 실패하고 실패 할 경우, 그것은 유지했을 경우, 지정된 direction를 조회하려고

    def go(self, direction): 
        return self.paths.get(direction, self.paths.get('*', self)) 
    

    그런 짓을하는 것이 좋습니다 동일에 Room.

    '*' 여기에서 나는 None을 사용합니다. 그러나 그것은 개인적인 취향의 문제라고 생각합니다.

+0

일반 죽음 사전을 4 개의 개별 방으로 대체했습니다. 각각 다른 방 경로에서 호출되었습니다. 그래서 나는 가야 방법을 바꾸어야합니까? 나는 왜 그것이 틀린 지 알지 못한다. 나는 강사로부터 그것을 얻었다. –

+0

'3'에서 제안한 go 메소드를 변경 한 후에도 동일한 오류가 발생합니다. –

+0

좋아, 나는 약간의 장난을 치고 나서 게임을하고, 몇 가지를 제거하고 세션 디렉토리를 정리해야했지만, 나는 당신의 제안으로 그것을 돌렸다. 3. 당신이 무엇이든 타이핑 할 수 있고, 오류를주지 마라. –

관련 문제