2015-10-15 64 views
-2

我正在嘗試製作基於文本的冒險遊戲,而且我還有幾天學習Python。我打算在我去的時候添加它,並用它來應用我正在學習的內容。如果while循環中的語句忽略列表

由於某些原因,雖然一切看起來都很好,但是Python在開始添加while循環時開始忽略我的「負面」列表。我無法弄清楚爲什麼!

# Error message printed if player types gibberish or doesn't pick one of the available responses. 
def error(): 
    print("You must choose one of the options!") 

# Allows going back to the beginning of the game should the player lose. 
def begin(): 
    playerName = input("Enter your name: ").upper() 
    print("This is a choose your own adventure game. %s, you have a great deal of danger ahead of you. This will require a lot of courage. Are you sure you are ready?" % (playerName)) 

# List of affirmative sayings the player may type (incl. alternatives to "yes"). 
affirmative = ["yes", 
     "y", 
     "yup", 
     "yep", 
     "sure", 
     "okay", 
     "rad", 
     "yepper", 
     "yeppers", 
     "alright", 
       ] 

# List of negative sayings a player may type (incl. alternatives to "no"). 
negative = ["no" 
     "n", 
     "nah", 
     "nu", 
     "nope", 
     "negative", 
     "negatory", 
     "go away", 
     ] 

begin() 

""" If they aren't ready, gives them an epilogue and allows them to restart. If they ARE ready, allows them to continue. If they type gibberish, it makes them choose between one of the two options. """ 

while True: 
    ans = input() 
    if ans in negative: 
     print("You never begin your journey. You stay at home and lead a completely normal, boring life. As the years go by, you find yourself reflecting more and more often on what could have been. The spectre of your unfulfilled potential haunts you until your death. :(") 
     newAns = input("Press Enter to start over.") 
     if newAns == True or newAns == False: 
      begin() 
      break 
    elif ans in affirmative: 
     print("You begin your journey into a dark wood. Are there any supplies you'd like to take with you?") 
     break 
    else: 
     print("You must choose! Yes... or no?") 
     continue 
+1

你在使用Python 2或3嗎? – Barmar

+0

在'''input'''中,是否在返回之前從用戶的響應中刪除空格? – wwii

+0

它以什麼方式忽略列表?這對我來說可以。當然,列表中沒有出現「不」的細節(您已經構建了法語「非」),但這並不能解釋完整的「忽略」。給出實際產出和預期結果。 – Prune

回答

3

你已經在你的negative列表錯位的no後一個逗號。

將其更改爲

negative = ["no", 
      "n", 
      "nah", 
      "nu", 
      "nope", 
      "negative", 
      "negatory", 
      "go away", 
      ] 

之前,non,因爲無論是在技術上名單將打印You must choose! Yes... or no?消息。儘管如此,其他回覆如nah仍然有效。添加逗號,你應該沒問題。

+1

爲了擴大你的觀點,Python語法'「string」「second」'被解析爲''stringsecond''因此,列表中沒有'no'和''n'' ,但「非」後跟「nah」。 – msw

+1

我沒有意識到這一點,謝謝你的加入! – rob