簡體   English   中英

為什么我的列表沒有變化?

[英]Why doesn't my list change?

為了娛樂,我一直在忙着玩一個小游戲,但我遇到了一個問題。 我將發布代碼並盡力解釋:

def parseCmd(string):
    cmd = string.split(' ')
    if cmd[0] == 'help':
        showHelp()
    elif cmd[0] == 'add':
        addServer()
    elif cmd[0] == 'bag':
        viewInventory(inventory)
    elif len(cmd) == 1 and cmd[0] == 'look':
        describeRoom()
    elif len(cmd) == 1 and cmd[0] == 'take':
        print 'What do you want me to take?'
    elif cmd[0] == 'take':
        pickUp(cmd[1],  items)
    elif cmd[0] == 'exit':
        sys.exit(0)
    else:
        print 'I don\'t know how to '  + cmd[0]

def describeRoom():
    print locations[player_location]

def pickUp(item,  item_list):
    if item in item_list[player_location]:
        item_list[player_location].remove(item)
        inventory.append(item)
        print 'You took the ' + item        
    else:
        print 'I can\'t find any ' + item

inventory = ['id card',  'money',  'keys']
player_location = 'cookieroom'
items = {'cookieroom': ['crowbar',  'hammer']}
locations = {'cookieroom': 'The cookieroom, where all the hard work gets done. \n\nNORTH: LFA - ITEMS: %s' % items[player_location], 
                'LFA': 'The infamous LFA, where dreams of office supplies become reality. there is a big guy sleeping in his chair next to a fire extinguisher.\n\nSOUTH: Cookieroom, WEST: WC'}

if __name__ == "__main__":
    while 1:
        t = raw_input('-> ')
        parseCmd(t)

因此,正如您所看到的,當您領取該特定房間中可用的項目時,我希望更改項目字典中的項目列表。 我可以拿起物品並將其添加到我的庫存中,但是如果我發出命令“ look”,它會以其原始狀態顯示物品清單。

我一直在谷歌搜索和stackoverflowing 1.5天,我找不到任何似乎可以解決此問題的方法。

如果不清楚,請問我,我會盡力回答。

程序啟動后,將對describeRoom函數從其獲取房間描述的locations字典進行初始化。 那時,玩家所在的位置是cookieroom ,而物體則是crowbarhammer 因此,像這樣創建一個字符串

'The cookieroom, where all the hard work gets done. \n\nNORTH: LFA - ITEMS: ["crowbar", "hammer"]'

即使您以后更改items字典的內容,該字符串也不會改變。

您的locations字典應僅包含房間說明的不變部分。 每當用戶要求描述房間時,都應重新計算變化部分(例如房間中的物品清單等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM