我的python游戏没有输出我想要的东西

时间:2014-09-13 01:37:17

标签: python loops printing while-loop function

输出生成是和否响应的打印答案。我想要它生成 一个答案为肯定,一个答案为否,如用户所述。

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

1 个答案:

答案 0 :(得分:1)

如果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

在代码中没有实际检查答案是肯定还是否的地方。因此,要相应地输出,请确保检查这些值。 (看看有问题的提示')

相关问题