r/pythonhelp • • Oct 04 '21

Super easy 2 minute question but I'm new to python coming over from c# and c++ and none of this makes sense, what the heck is wrong with this? I'm trying to get N to lead to ending the loop as well as any response that isn't Y, thanks.

isPlaying = True
while isPlaying:
answer = input("Do you want to keep playing?")
if answer == "Y":
print("Okay")
elif answer == "N":
print("Goodbye")
isPlaying = False
else:
isPlaying = False

2 Upvotes

3 comments sorted by

1

u/Brilliant_Fall8987 Oct 04 '21

You forgot a = in second and third is playing but i think that s just do to quick typing try elif answer !="y" and answer!="n" And i don t see any mistakes in your code

1

u/alonbit1106 Oct 04 '21 edited Oct 04 '21
isPlaying = True
while isPlaying: 
    answer = input("Do you want to keep playing?") 
    if answer.lower() == "y": 
        print("okay") 
    elif answer.lower() == "n": 
        print("Goodbye") 
        isPlaying = False 
        break 
    elif answer.lower() != "Y" or "N": 
        print("type Y or N") 
        break 
    else: 
        isPlaying = True

I believe this would help you! if it isn't what you are looking for tell me.

if you want the game to continue after you wrote any character that is not y or n

replace break in line 12 with continue

(i added .lower so it will also work for lowercase n and y, just a little improvement, if you want to remove it then just delete the .lower and done.)

1

u/MT1961 Oct 04 '21

Hm. Not sure what you are asking. Formatting is important in Python, so assuming your code looks like this:

isPlaying = True

while isPlaying:

answer = input("Do you want to keep playing?")

if answer == "Y":

print("Okay")

elif answer == "N":

print("Goodbye")

isPlaying = False

else:

isPlaying = False

It should work fine. Indentation is the same as {} in Java or C# or even C++