英文:
Looking for an explanation of why this block does not work
问题
我刚开始学习编程,正在尝试各种方法。可能是一个愚蠢的问题,但为什么这个代码块无论我输入什么都只返回0值?
start = input("Would you like to play? (Y/N)\n")
if start == "N" or start == "n":
game_state = 0
elif start == "Y" or start == "y":
game_state = 1
我尝试了各种方式来更改它,但似乎无法使它正常工作。
英文:
I am just starting to learn to code and I am experimenting with all sorts of methods. Might be a dumb question but why does this block only return the 0 value regardless of what I input?
start = input("Would you like to play? (Y/N)\n")
if start == "N" or "n":
game_state = 0
elif start == "Y" or "y":
game_state = 1
I have tried to change it in all sort of ways but I can t seem to make it work.
答案1
得分: 1
需要将您的if语句更改为:
if start == "N" or start == "n":
或者:
if start.lower() == "n":
非空字符串将评估为True,因此第一个if语句将始终运行。
英文:
You need to change your if statements to:
if start == "N" or start == "n":
or to:
if start.lower() == "n":
A non-empty string will evaluate to True, so the first if statement will always run.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论