“equality operator == not working correctly” 可以翻译为 “等号操作符 == 不正常工作”。

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

equality operator == not working correctly

问题

I'm trying to write a simple text-based game. Everything seems to be working. At the end, I want to add an option to play again. I put the whole thing in a loop and at the end, I added:

again = input('Would you like to play again? (Y/N) ')
if again == "Y" or again == "y":
    continue
else:
    break

Input seems to be storing the correct input but when compared to 'Y' or 'y' returns true no matter what the input is.

I confirmed that the input is correct. Edited the code to visualize it better.

again = input('Would you like to play again? (Y/N) ')
print(again)
if again == "Y" or again == "y":
    print("True")
else:
    print("False")

This was the output which didn't make sense to me.

Would you like to play again? (Y/N) n
n
True
英文:

I'm trying to write a simple text-based game. Everything seems to be working. At the end, I want to add an option to play again. I put the whole thing in a loop and at the end, I added:

again = input('Would you like to play again? (Y/N) ')
if again == "Y" or "y":
    continue
else:
    break

Input seems to be storing the correct input but when compared to 'Y' or 'y' returns true no matter what the input is.

I confirmed that the input is correct. Edited the code to visualize it better.

again = input('Would you like to play again? (Y/N) ')
print(again)
if again == "Y" or "y":
    print("True")
else:
    print("False")

This was the output which didn't make sense to me.

Would you like to play again? (Y/N) n
n
True

答案1

得分: 1

请将以下内容更改:

if again == "Y" or "y":

改为:

if again == "Y" or again == "y":
英文:

Try changing:

if again == "Y" or "y":

to:

if again == "Y" or again == "y":

答案2

得分: -2

if again == 'Y' or again == 'y':

英文:
if again == 'Y' or again == 'y':

huangapple
  • 本文由 发表于 2023年6月30日 01:59:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583546.html
匿名

发表评论

匿名网友

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

确定