我的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中非常非常重要。请以可执行的格式呈现代码。