Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的python游戏赢了';我不能输出我想要的_Python_Loops_Printing_While Loop_Function - Fatal编程技术网

我的python游戏赢了';我不能输出我想要的

我的python游戏赢了';我不能输出我想要的,python,loops,printing,while-loop,function,Python,Loops,Printing,While Loop,Function,输出将生成“是”和“否”响应的打印答案。我希望它能产生 一个答案表示是,一个答案表示否,由用户输入 import time import random time.sleep(0) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") print("Welcome to the zombie apocalypse simulator!") print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") p

输出将生成“是”和“否”响应的打印答案。我希望它能产生 一个答案表示是,一个答案表示否,由用户输入

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

如果回答是,那么
在['Yes'、…'No']中返回答案
返回
True
否则
False
。基本上,中的
将告诉您列表是否包含给定元素。因此,在
ask
函数中,如果用户输入一个可能的答案,则返回一个
True
值,使下一个while循环的行为类似

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

代码中没有任何地方可以检查答案是“是”还是“否”。因此,要相应地输出,请确保检查这些值。(请看问题注释中的提示)

提示:列表总是正确的,除非它是空列表
[]
。所以
如果['Yes','Yes','y','y','Yes']
总是正确的。您需要修复
ask()
函数,因为
中的
不像您想象的那样工作。请修正代码的缩进。注意,缩进在Python中非常非常重要。请以可执行的格式呈现代码。