0

So for my first big python project I'm building a text based game. 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'. After the command is interpreted, it goes to the execution module called 'item_or_loc' It's here that I get the error. 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)
4

1 回答 1

4

您不会从items.items_f. 您需要返回一个容器或序列。我会建议与下面不同的方法,但至少这是一个开始。

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)

解释一下,items_f它不是容器本身,而是一个函数(或更准确地说,一种方法,您可以简单地将其视为“附加”到对象的函数)。函数不必返回任何东西,但是当你调用它们时,调用产生的值只是None.

现在,当您进行类似的测试时if x in y:y必须是序列或容器类型;并且由于您正在测试 function 的结果items_f,并且由于该函数None按照您在上面定义的方式返回,因此测试会引发错误。

处理这种情况的最佳方法实际上取决于程序的更大结构。朝着正确方向迈出的第一步可能是这样的:

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

但这也可能不是最好的解决方案。根据您在上面添加的内容(顺便说一句,现在缺少该items_f功能),我的建议是使用某种包含项目的数据结构。一个简单的方法是字典:

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

这将创建一个包含所有可能项的字典。现在当你想要一个特定的项目时,你可以像这样得到它:

item = items['patient_gown']

这样你根本不需要函数;您可以直接访问整个字典。

于 2012-04-13T21:43:25.917 回答