2012-04-13 125 views
0

因此,對於我的第一個大型python項目,我正在構建一個基於文本的遊戲。它應該是模塊化的,故事和項目等可以編輯並替換爲實際源代碼的少量編輯。基本上,用戶命令存儲爲一個立即分解爲列表的字符串。第一個元素是像'檢查'這樣的動作,第二個元素是像「位置」或「項目」那樣的動作的僞參數。在解釋命令後,它會進入名爲'item_or_loc'的執行模塊。在這裏,我得到錯誤。誰能幫忙?如果它能提供幫助,我會提供更多信息或整個源代碼。Python TypeError:'NoneType'類型的參數不可迭代

命令模塊:

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) 

項目模塊:

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','') 

位置模塊:基本上一樣的項目模塊

播放器模塊:

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) 
+2

以源代碼的形式提供更多上下文會非常有幫助。這很難幫助你。 – 2012-04-13 21:38:39

+0

你想要多少錢?整個command.py模塊? – SciurusDoomus 2012-04-13 21:39:08

+0

至少要說明「物品」來自何處。 – 2012-04-13 21:39:34

回答

3

你不「T從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必須是序列或容器類型;並且由於您正在測試函數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'] 

這樣你就不需要函數了;你可以直接訪問整個字典。

+0

刪除我的答案,因爲我們兩個人中沒有太多意思是說同樣的事情。 – 2012-04-13 21:45:48

+0

嗯,我的意思是我寧願不把所有的項目列表名稱輸入到一個元組或列表或任何其他項目中。 (我發現一個元組可能會更好,因爲它是不可改變的。) – SciurusDoomus 2012-04-13 21:54:58

+0

@DavidHeffernan,對不起,我踩了你的答案;我只是看到了一個增加更多信息的機會,並沒有真正想過。 – senderle 2012-04-13 22:16:19

相關問題