【问题标题】:Python, can't find functionPython,找不到函数
【发布时间】:2013-06-19 02:48:03
【问题描述】:

目前我正在学习 Python 的在线课程,只完成了大约 1/3 的课程,我决定尝试用我目前所学的知识来做一些事情。虽然现在遇到错误。我正在房子里创建一个基于文本的冒险游戏。每个房间都是独立的功能。 例如:

def hallway():
hallway_direction = raw_input('blahblah')
if hallway_direction == 'n':
    living_room()

虽然我有一个房间需要手电筒才能进入。我使用字典来保存房间的任何值,这就是我所拥有的。

global rooms

rooms = {}
rooms['first_room'] = {'note' : False}
rooms['old_door'] = {'boots' : False}
rooms['first_again'] = {'torch' : False}
rooms['first_again'] = {'seen' : False}

在另一个房间里,它把 torch 设置为 true,但我遇到的问题是,如果你没有手电筒,我需要它带你回到大厅

def fancy_door():
    raw_input('You open the door, the inside is pitch black. You need a source of light before you can enter.')
    if rooms['first_again']['torch']:
        raw_input('You light the torch and step inside, the room is bare, only a table with a ring in the center.')
        choice5_r = raw_input('Do you take the ring? Y/N ("back" to leave)')
        choice5_r = choice5_r.lower()
        if choice5_r == 'y':
            raw_input('Some text here')
            darkness()
        elif choice5_r == 'n':
            raw_input('You leave the ring as it is.')
            fancy_door()
        elif choice5_r == 'back':
            hall()
        else:
            raw_input('Not a valid option')
            fancy_door()
    else:
        hall()

当我运行它时,我得到了这个错误:

Traceback (most recent call last):
File "<stdin>", line 247, in <module>
File "<stdin>", line 23, in first_room
File "<stdin>", line 57, in hall
File "<stdin>", line 136, in fancy_door
KeyError: 'torch'

在第 247 行,它调用了 first_room(),它一直有效。 23 调用 hall() 直到这一点。 57 调用了应该可以工作的 fancy_door() ,它看起来与其他门功能相同,并且工作正常。第 136 行是“if rooms['first_again']['torch']:”上方的行

如果问题不在这里,我可以在此处或 pastebin 上发布整个代码,我没有这样做只是因为它有 230 行长。

如果有人可以帮助我,我会非常感激。

另外,请原谅糟糕的代码,我知道它可能不遵循正确的约定,但就像我说的,我是 Python 新手,一般来说都是编程新手。这是我写过的第一件事。 提前致谢!

【问题讨论】:

    标签: python function dictionary traceback


    【解决方案1】:

    在全局变量的定义中,您定义了 rooms['first_again'] 两次。

    每次为字典的元素赋值时:

    rooms['first_again'] = #something
    

    你会覆盖之前的内容。

    这是说

    KeyError: 'torch'
    

    因为该对象不再具有名为 torch 的元素。

    尝试将其更改为:

    rooms['first_again'] = {'torch' : False, 'seen' : False}
    

    或者,如果您稍后需要向该元素添加值,您可以这样做:

    rooms['first_again'] = {'torch' : False}
    rooms['first_again']['seen'] = False
    

    【讨论】:

    【解决方案2】:

    您已经分配了两次rooms['first_again']

    rooms['first_again'] = {'torch' : False}
    rooms['first_again'] = {'seen' : False}
    

    也许应该是:

    rooms['first_aggin'] = {}
    rooms['first_again']['torch'] = False
    rooms['first_again']['seen'] = False
    

    【讨论】:

      【解决方案3】:

      这里的线索是KeyError: 'torch'。当您尝试访问不存在的字典中的键时会发生此错误。

      看起来问题在于你如何处理rooms['first_again']。您显示以下代码:

      rooms['first_again'] = {'torch' : False}
      rooms['first_again'] = {'seen' : False}
      

      rooms 是一个有多个键的字典,其中一个是'first_again'。当您引用rooms['first_again'] 时,您将检索与此键对应的对象。你在这里实际上做的是嵌套另一个字典。

      第一个赋值将字典{'torch' : False} 分配给rooms['first_again']。第二个赋值的作用非常相似,但它覆盖了第一个赋值。包含您的 torch 值的对象不再存在!

      如果您想在同一个键上有多个可用值,请将这些值放在一个字典中。

      rooms['first_again'] = { 'torch' : False, 'seen' : False }
      

      现在您可以完全按照您在原始代码中尝试的方式引用这些值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-28
        • 2021-01-23
        • 2019-05-31
        • 2022-10-05
        • 1970-01-01
        • 2019-01-08
        相关资源
        最近更新 更多