英文:
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':
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论