繁体   English   中英

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

[英]My python game won't output what I want

输出会为是和否响应生成打印答案。 我希望它根据用户的输入生成一个肯定的答案和一个否定的答案。

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

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

代码中没有实际检查答案是或否的地方。 因此,要进行相应输出,请确保检查这些值。 (查看问题评论中的提示)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM