2013-07-08 168 views
1

我正在創造一個文本RPG的靈感來自舊的文字冒險,玩家輸入英文命令;如「拿起劍」等。 我已經建立了一個簡單的;輸入'A'來做到這一點,並輸入'B'來做到這一點,但我想擴大我的系統以獲得更多的自由。 我需要創建一個系統;當玩家鍵入一個命令時,程序挑選出關鍵詞。 我認爲這可以通過'in'命令實現。 這裏是我的代碼:英文命令[文字RPG] - 蟒蛇

print "What would you like to do??" 
input_loop_sleep = str('') 
choice_sleep = raw_input(str('>>>')) 
loop_sleep = False 
table_time = False 
bed_time = False 
error_time = False 



while loop_sleep == False: 


    if str('sleep') in choice_sleep or str('bed') in choice_sleep or str('goodnight') in choice_sleep or str('Sleep') in choice_sleep or str('tired') in choice_sleep: 
     while bed_time == False: 
      print "you decide to go back to sleep" 
      time.sleep(1) 
      print "..." 
      time.sleep(1) 
      print "" 
      time.sleep(1) 
      print "darkness" 
      time.sleep(1) 
      print "" 
      print "you wake up..." 
      time.sleep(1) 
      print "it is now 9:15am" 
      time == int(9.15) 
      time.sleep(1) 
      print "You are standing in your room, slightly more refreshed." 
      time.sleep(1) 
      print "there is a table with some things on it, stairs, and a wardrobe... with the doors wide open..." 
      time.sleep(1) 
      print "that's strange... you swear that they were shut when you went to sleep..." 
      break 
     else: 
      bed_time == True 
     break 
     bed_loop_choice = raw_input('>>>') 



    elif str('bedside') in choice_sleep or str('table') in str(choice_sleep): 
     while table_time == False: 
      print "You rub your eyes and pick up some belongings from a" 
      print "bedside table." 
      time.sleep(1) 
      print "Map added!" 
      time.sleep(1) 
      print "100 gold added!" 
      time.sleep(1) 
      print "Leather Bag added!" 
      cash == int(100) 
      time.sleep(1) 
      Map == str('map of', str(province)) 
      Inventory == [str(Map)] 
      container == str('leather bag') 
      print "your", str(container), str("contains a"), str(Map), str('and'), str(cash) 
      break 
     else: 
      table_time == True 
     break 

    else: 
     print "invalid command!" 
當我運行的代碼

,無論我在裏面輸入的內容總是與「睡眠」選項進入。 我可能只是犯了一些簡單的錯誤! 你能幫我解決我做錯了什麼,以及我如何解決它。

+3

下載不公開。請儘量讓您的問題自成一體。 –

+0

請詳細描述您的問題,並在此發佈您的相關代碼。 –

+0

修復了問題 – Dannyboy8899

回答

5

要回答你關於爲什麼sleep循環重複所有的時間問題:

你控制迴路通過

while bed_time == False: 

,但你永遠不會在你的循環設置bed_timeTrue(僅在else子句,但該子句僅在環路正常退出時執行,而不是,因爲您現在正在執行此操作,因此bed_time將永遠不會更改)。

此外,直接比較布爾值通常是皺眉。慣用的方式(大多數語言,不僅僅是Python)應該是while not bedtime:

在開始這樣一個大項目之前,您應該閱讀一些初學者的編程書籍和/或Python tutorial。代碼中有幾個問題表達了您需要掌握一些基本編程原則和Python習慣用法的印象。

例如,

int(9.15) 

不是存儲時間的好方法 - 其結果將是9

然後,您使用time == int(9.15),這意味着「將模塊time與整數9進行比較」。我想你的意思是time = int(9.15)由於上述原因,這已經不好了,但是還會有另外一個問題:你將覆蓋模塊名稱time,這將導致後續的time.sleep(1)命令失敗並返回AttributeError

在您的代碼中不需要調用大多數str()調用,因爲您已在字符串的對象上使用它。你不在的地方,這是不正確的:str('map of', str(province))將會增加TypeErrorstr只帶一個參數)。

您正在使用不是類實例的對象的大寫變量名稱。

等等...

+2

我還會注意到==不是Python中的賦值運算符,只是比較運算符。 – llb

+0

@llb:哇,我完全錯過了。 –

2

我想,這應該足以理清問題

In [1]: str('bed') in "bedside" 
Out[1]: True 

所以,當你寫bedside它得到了sleep選項裏面,如果狀態,因此你得到錯誤的答案。

你應該寫:

if str('bed') == choice_sleep or *other conditions* : 
    then got inside the sleep option 

P.S:我假設你已導入time模塊。 P.P.S:我檢查了輸入table的代碼,它工作正常。