我的python游戏不会输出我想要的内容

问题描述:

输出会为yes和no响应生成打印答案。我希望它能生成 一个是的答案,一个是否的答案,正如用户输入的那样。我的python游戏不会输出我想要的内容

import time 
import random 
time.sleep(0) 
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") 
print("Welcome to the zombie apocalypse simulator!") 
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") 
print() 
time.sleep(0) 


print("You wake up in the backseat of an old 1995 suburu, shirtless and confused.") 
time.sleep(0) 
print("Looking out the dusty window, you can make out the skyline of a distant") 
print("forgotten metropolis.") 
print() 
time.sleep(0) 

def ask(question): 
    answer = input(question + " {y/n}") 
    return answer in ['Yes','YES','y', 'Y','yes', 'n', 'N', 'no', 'NO', 'No'] 

while ask("You see a backpack in the from seat of the car. Do you take it?"): 
    if ['Yes','YES','y', 'Y','yes']: 
     print("You look inside only to find a brown hoodie, a utility knife, a can of tuna") 
     print("and a half full water bottle.") 
    if ['n', 'N', 'no', 'NO', 'No']: 
     print("You leave the bag behind.") 
    break 
+0

提示:列表总是为真,除非它是空列表[[]'。所以'如果['是','是','y','是','是']'总是为真。你需要修正你的'ask()'函数,因为'in'不像你似乎认为的那样工作。 PS。请修正您的代码的缩进。 – 2014-09-13 02:10:18

+0

请注意缩进在Python中确实很重要。请以可执行的格式展示代码。 – 2014-09-13 02:33:19

如果answer = 'YES'然后 return answer in ['Yes', .. , 'No']回报True否则False。基本上,in会告诉你列表是否包含给定的元素。所以,在你ask功能,如果用户输入返回一个True值可能的答案之一,使下一个while循环表现得像

# ask question, if answer is positive 
while True: 
    # check if yes, do something 
    # else do something. 
    break 

有,你实际上是在检查所述代码内没有地方答案是是或否。因此,相应地输出确保您检查这些值。 (请看提问的评论)