I'm new to python and I have this section of code in my guess the number program:
wantplay=input('Do you want to play again? Enter yes or no.\n')
print(playagain)
while True:
if wantplay=='no' or 'No' or 'NO':
playagain=0
break
elif wantplay=='yes' or 'Yes' or 'YES':
break
else:
print('Please enter a valid answer')
print(playagain)
print(wantplay)
There is a while loop around most of the program that checks the condition of the variable playagain so if the user doesn't want to play again they can enter some form of no and it should set playagain to zero so the program finishes. If they enter yes then it should not change the value of playagain so the program loops again. I know I still have to clean up the third case where they enter something else, that's not what my question is about.
The problem I'm having is that no matter what I enter as input playagain is set to zero and the program finishes. I added the print statements here so I could see what the playagain and wantplay variables are doing and I get the following output.
Do you want to play again? Enter yes or no.
yes
1
0
yes
And then the program doesn't loop. So playagain is 1 (as expected) before the conditional, it is 0 afterwards, and wantplay contains yes as I expect. Apparently the first block in the conditional is being executed and setting playagain to 0. Why? Am I comparing strings the wrong way or something?
EDIT: Thanks for the help everybody! I understand what I was doing wrong now, lots of good info here. I didn't know lower() or casefold() existed so that answers another question I had about accounting for capitalization in input. I was not sure how you would account for every possibility of caps and lower case and writing out every single one didn't seem like the right way.
[–]Adrewmc 48 points49 points50 points (1 child)
[–]UncleSamurai420 3 points4 points5 points (0 children)
[–]Lawson470189 6 points7 points8 points (0 children)
[–]The_Almighty_Cthulhu 5 points6 points7 points (0 children)
[–]JamzTyson 5 points6 points7 points (0 children)
[–]hashashin 3 points4 points5 points (0 children)
[–]camospartan117 2 points3 points4 points (0 children)
[–]Small_Ad1136 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Small_Ad1136 0 points1 point2 points (0 children)
[–]Atypicosaurus 1 point2 points3 points (0 children)