循环显示正确答案,如

2024-04-18 20:10:38 发布

您现在位置:Python中文网/ 问答频道 /正文

问题回答正确。你知道吗


1条回答
网友
1楼 · 发布于 2024-04-18 20:10:38

将if语句更改为:

print("Hello there " + str(name) + ", are you ready for your 
adventure? Y/N")
adventure = input()
while adventure.lower() not in ("y", "n"): # <<<<<   This line changed

    print(str(name) + " ,that's not a choice. I'll ask again, are 
you ready for your adventure? Y/N")
    adventure = input()
    if adventure.lower() == "n":
        print("Cowards take the way out quickly.")
        breakpoint
    else:
        print("Come, you will make a fine explorer for the empire!")

这是由于Python3中的比较是如何完成的。见here

您可以对代码进行一些其他修复:


adventure = input("Hello there {}, are you ready for your adventure? Y/N".format(name)) #Added prompt to input, using string formatting. 

while adventure.lower() not in ("y", "n"): # <<<<<   Check input against tuple, instead of using `or` statement

    adventure = input(" {}, that's not a choice. I'll ask again, are you ready for your adventure? Y/N".format(name)) #Same as first line
    if adventure.lower() == "n":
        print("Cowards take the way out quickly.")
        break
    else:
        print("Come, you will make a fine explorer for the empire!")

usage of python input commandstring formatting

相关问题 更多 >