2015-10-15 35 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