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

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

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:

  1. again = input('Would you like to play again? (Y/N) ')
  2. if again == "Y" or again == "y":
  3. continue
  4. else:
  5. 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.

  1. again = input('Would you like to play again? (Y/N) ')
  2. print(again)
  3. if again == "Y" or again == "y":
  4. print("True")
  5. else:
  6. print("False")

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

  1. Would you like to play again? (Y/N) n
  2. n
  3. 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:

  1. again = input('Would you like to play again? (Y/N) ')
  2. if again == "Y" or "y":
  3. continue
  4. else:
  5. 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.

  1. again = input('Would you like to play again? (Y/N) ')
  2. print(again)
  3. if again == "Y" or "y":
  4. print("True")
  5. else:
  6. print("False")

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

  1. Would you like to play again? (Y/N) n
  2. n
  3. True

答案1

得分: 1

请将以下内容更改:

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

改为:

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

Try changing:

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

to:

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

答案2

得分: -2

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

英文:
  1. 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:

确定