gpt4 book ai didi

python - 如何解开一根绳子?

转载 作者:行者123 更新时间:2023-12-01 05:43:42 25 4
gpt4 key购买 nike

我需要调用一个对象的方法,但我不断收到错误:

“str”对象没有属性“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 都有路径,go 应该将您移动到您在表单中选择的任何路径。我该如何修复这个错误?

我想去新房间并渲染它。问题出在

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

这是整个 app.py 文件:

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 文件:

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

最佳答案

正如我在上面的评论中所怀疑的:

您犯了几个错误,导致您偶然遇到或尚未遇到的错误。

请记住,您的路径字典只允许包含 Room 对象。

但在几个地方,你还有其他东西:

  1. 所有路径在某处都有一个 generic_death[rn] - 这是一个字符串,而不是 Room 对象。你应该改变这一点。

  2. 这样做会更好

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

    如果执行的操作不存在,则为了留在同一个房间。

  3. 您的某些 dict 具有 '*' 键。如果您想实现“在所有其他情况下”条目,我建议执行类似的操作

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

    它尝试查找给定的方向,如果失败,它会寻找'*',如果失败,它会停留在同一个房间.

    '*' 在这里没问题,我不喜欢使用 None。但我认为这是个人品味问题。

关于python - 如何解开一根绳子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16797877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com