为什么这个代码块不起作用的解释。

huangapple go评论71阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月7日 01:49:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定