Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;类型错误:';非类型';对象不可下标";在我的游戏里找一本字典?_Python_Python 3.x_Iteration_Nonetype - Fatal编程技术网

Python &引用;类型错误:';非类型';对象不可下标";在我的游戏里找一本字典?

Python &引用;类型错误:';非类型';对象不可下标";在我的游戏里找一本字典?,python,python-3.x,iteration,nonetype,Python,Python 3.x,Iteration,Nonetype,我现在正在学习Python入门课程,我终于解决了我们正在开发的这个冒险游戏的所有语法错误。除了类的项目蓝图中提供的引擎之外,我编写了所有代码。我想,因为我已经解决了语法问题,游戏将接近正常运行,但当我尝试运行游戏时,我得到的错误是“TypeError:‘NoneType’对象不可订阅”。我们不应该弄乱游戏的引擎,因为我们应该作为类的一部分来解决它,但引擎似乎是导致问题的原因。我只是不理解与我的问题有关的错误代码 整个项目在这里:,但这是导致错误的特定部分 def main(): prin

我现在正在学习Python入门课程,我终于解决了我们正在开发的这个冒险游戏的所有语法错误。除了类的项目蓝图中提供的引擎之外,我编写了所有代码。我想,因为我已经解决了语法问题,游戏将接近正常运行,但当我尝试运行游戏时,我得到的错误是“TypeError:‘NoneType’对象不可订阅”。我们不应该弄乱游戏的引擎,因为我们应该作为类的一部分来解决它,但引擎似乎是导致问题的原因。我只是不理解与我的问题有关的错误代码

整个项目在这里:,但这是导致错误的特定部分

def main():
    print(render_introduction())
    world = create_world()
    while world['status'] == 'playing':
        print(render(world))  #line 316
        options = get_options(world)
        command = choose(options)
        print(update(world, command))
    print(render_ending(world))
if __name__ == '__main__':
    main() #line323
我得到的全部错误是

Traceback (most recent call last):
    File "C:\Users\\Appalachian Trail Legit.py", line 323, in <module> main()
    File "C:\Users\\Appalachian Trail Legit.py", line 316, in main while world['status'] == 'playing':
TypeError: 'NoneType' object is not subscriptable
回溯(最近一次呼叫最后一次):
main()中第323行的文件“C:\Users\\Appalachian Trail Legit.py”
文件“C:\Users\\Appalachian Trail Legit.py”,第316行,在主文件中,while world['status']=='playing':
TypeError:“非类型”对象不可下标
我假设整个游戏都被破坏了,但老实说,我甚至不知道从哪里开始修复它。我不太确定哪个变量变成了
None
,我想错误代码是说
world['status']
None
,但是
['status']
在游戏启动后就被设置为
'playing'
。也许一些训练有素的人可以看到这个问题


编辑:非常感谢@aaron指出这是缩进错误。避免TypeError的最佳方法是测试:“非类型”对象不可下标

错误显示,正在订阅不可订阅的数据类型。订阅是指通过索引或值引用数据结构中的值。对于列表、元组等,使用索引完成订阅,对于字典,使用键值完成订阅

这是一个字典订阅的示例

sample_dict={100:'a',200:'b'}
是一本字典。 为了从本词典中获取值,请使用键值引用。因此,sample_dict[100]产生“a”和类似的输出

在程序中出现错误时,
world['status']
的订阅提取失败。这是因为变量
world
不是预期的
dictionary
,而是一种
None
类型,它只是说变量内部没有任何内容

要解决此问题,请了解
world
变量是如何分配的。从程序中我看到它来自语句
world=create\u world()

检查
create_World()
函数并解决错误

def main():
    print(render_introduction())
    world = create_world()
    while world['status'] == 'playing':
        print(render(world))  #line 316
        options = get_options(world)
        command = choose(options)
        print(update(world, command))
    print(render_ending(world))
if __name__ == '__main__':
    main() #line323

祝您愉快。:)

在create_world()函数末尾返回的内容必须是字典。 我经常在create_map()函数的返回字典中遇到这个问题

return {
        'map' : create_map(),
        'player' : create_player(),
        'status' : 'playing'
     }
根据我的经验,这总是令人讨厌。如果您计划再次调用返回值,那么您要做的是在传递这些函数之前分配它们。因此,对于代码中看起来像这样的所有内容,您可能希望使其更像

map = create_map()
player = create_player()
return {
        'map' : map,
        'player' : player,
        'status' : 'playing'
     }
注意,这些是变量名,因此没有引号。这样做,你就会没事了。
我希望这对您有所帮助。

您缺少嵌套函数
create\u map
的缩进,该函数过早结束了
create\u world
函数,并隐式返回
None

def create_world():
...
def create_player():
返回。。。
def create_map():
返回。。。
返回{
“映射”:创建映射(),
“player”:创建_player(),
“状态”:“正在播放”
}
应该是:

def create_world():
...
def create_player():
返回。。。
def create_map():
返回。。。
返回{
“映射”:创建映射(),
“player”:创建_player(),
“状态”:“正在播放”
}

您的嵌套函数
create\u map
缺少缩进,该函数过早地结束了
create\u world
函数,并使其返回
None
@aaron祝福您的甜蜜心灵传奇