简体   繁体   English

Python TypeError:“ NoneType”类型的参数不可迭代

[英]Python TypeError: argument of type 'NoneType' is not iterable

So for my first big python project I'm building a text based game. 因此,对于我的第一个大型python项目,我正在构建一个基于文本的游戏。 It's supposed to be modular so the story and items etc. can be edited and replaced with little editing of the actual source code. 它应该是模块化的,因此故事和项目等可以进行编辑,而只需对实际源代码进行少量编辑即可替换。 Basically, the user command is stored as a string which is immediately broken into a list. 基本上,用户命令存储为字符串,该字符串立即分解为列表。 The first element is an action like 'inspect' and the second element is a pseudo-argument for that action like 'location' or 'item'. 第一个元素是类似于“检查”的动作,第二个元素是针对该动作的伪参数,例如“ location”或“ item”。 After the command is interpreted, it goes to the execution module called 'item_or_loc' It's here that I get the error. 解释该命令后,将转到名为“ item_or_loc”的执行模块。在这里,我得到了错误。 Can anyone help? 有人可以帮忙吗? I'll provide more info or the entire source code if it'll help. 如果有帮助,我将提供更多信息或整个源代码。

Command module: 命令模块:

    def item_or_loc(iolo):
        if iolo in items.items_f():
            print (items.iolo(1))
        elif iolo in locations.locations_f():
            print (locations.iolo(1))
        else:
            print ('Command not recognized, try again.')


    def location(loco):
        plo_l = PlayerClass #(player location object_location)
        if loco == 'location':
            plo_l.player_loc(0)


    def abort(abo):
        sys.exit()


    def inventory(invo):
        pio_i = PlayerClass #(player inventory object_inventory)
        if invo == 'inventory':
            pio_i.player_inv(0)

Items module: 项目模块:

    patient_gown=('Patient gown', 'A light blue patient\'s gown.')
    wrench=('Wrench','')
    stick=('Stick','')
    prybar=('Prybar','')
    screwdriver=('Screwdriver','')
    scalpel=('Scalpel','')
    broken_tile=('Broken tile','')
    hatchet=('Hatchet','')
    janitor_suit=('Janitor suit','')

Locations module: Basically the same as the Items module 位置模块:与项目模块基本相同

Player module: 播放器模块:

    import items
    import locations

    class PlayerClass:
        def player_inv(inv_index):
        pinventory = [items.patient_gown[inv_index]]
        print (pinventory)

    def player_loc(loc_index):
        ploc = [locations.cell[loc_index]]
        print (ploc)

You don't return anything from items.items_f . 您不会从items.items_f返回任何items.items_f You need to return a container or sequence. 您需要返回一个容器或序列。 I would advise a different approach from the below, but it's a start, at least. 我会建议一种与下面不同的方法,但这至少是一个开始。

def items_f():
    patient_gown=('Patient gown','A light blue patient\'s gown.')
    wrench=('','')
    stick=('','')
    crowbar=('','')
    screwdriver=('','')
    scalpel=('','')
    broken_tile=('','')
    hatchet=('','')
    janitor_suit=('','')
    return (patient_gown, wrench, stick, crowbar, 
            screwdriver, scalpel, broken_tile, hatchet, janitor_suit)

To explain, items_f is not a container itself, but is rather a function (or more precisely, a method, which you can think of simply as a function "attached" to an object). 为了说明, items_f本身不是容器,而是一个函数(或更准确地说,是一个方法,您可以简单地将其视为“附加”到对象的函数)。 Functions don't have to return anything, but then when you call them, the value that results from the call is simply None . 函数没有返回任何东西,但是当你给他们打电话,从电话产生的价值是根本None

Now, when you do a test like if x in y: , y has to be a sequence or container type; 现在,当您进行类似if x in y:的测试时, y必须是序列或容器类型。 and since you're testing the result of the function items_f , and since that function returns None as you've defined it above, the test throws an error. 并且由于您正在测试items_f函数的结果,并且由于该函数如上所定义返回None ,因此测试将引发错误。

The best way to handle this situation really depends on the larger structure of the program. 处理这种情况的最佳方法实际上取决于程序的较大结构。 A first step in the right direction might be something like this: 朝正确方向迈出的第一步可能是这样的:

def items_f():
    return (('Patient gown', 'A light blue patient\'s gown.'),
            ('Wrench', 'A crescent wrench.'),
            ('Stick', '...'))

But that's probably not the best solution either. 但这也不是最好的解决方案。 My suggestion based on what you've added above (which, by the way, is now missing the items_f function) would be to use a data structure of some kind that holds items. 基于您在上面添加的内容(顺便说一下,现在缺少items_f函数),我的建议是使用某种保存项目的数据结构。 A simple approach would be a dictionary: 一种简单的方法是使用字典:

items = {'patient_gown':('Patient gown', 'A light blue patient\'s gown.'),
         'wrench':('Wrench', '...'),
          ...
        }

This creates a dictionary that contains all possible items. 这将创建一个包含所有可能项目的字典。 Now when you want a particular item, you can get it like so: 现在,当您想要一个特定的项目时,您可以像这样获得它:

item = items['patient_gown']

This way you don't need a function at all; 这样,您根本不需要功能。 you can just access the whole dictionary directly. 您可以直接访问整个词典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:“ NoneType”类型的参数不可迭代python - TypeError: argument of type 'NoneType' is not iterable python Python聊天机器人“ TypeError:'NoneType'类型的参数不可迭代” - Python chatbot “TypeError: argument of type 'NoneType' is not iterable” Python - TypeError:“NoneType”类型的参数不可迭代 - Python - TypeError: argument of type 'NoneType' is not iterable Python游戏| TypeError:“ NoneType”类型的参数不可迭代 - Python Game | TypeError: argument of type 'NoneType' is not iterable python错误TypeError:'NoneType'类型的参数不可迭代 - The python error TypeError: argument of type 'NoneType' is not iterable TypeError:“NoneType”类型的参数在 python 中不可迭代 - TypeError: argument of type 'NoneType' is not iterable in python TypeError:NoneType 类型的参数在 python 中不可迭代 - TypeError: argument of type NoneType is not iterable in python TypeError:“NoneType”类型的参数不可迭代 - TypeError: argument of type 'NoneType' is not iterable “TypeError:'NoneType' 类型的参数不可迭代”? - “TypeError: argument of type 'NoneType' is not iterable”? 使用Python进行网络抓取:TypeError:“ NoneType”类型的参数不可迭代 - Using Python for web scraping: TypeError: argument of type 'NoneType' is not iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM