mardi 1 juin 2021

"else" option alternative is ignored in a while loop

This is probably a simple problem that requires a simple answer...but I have been scratching my head why this happens. It's a problem on the loop that I am testing. Here's the loop:

done = False
while done==False:
    checkUser = input("Do you want to continue?\nType Y or y to continue; any other char to quit\nType here: ")
    print(checkUser) # just to check the input

    if checkUser =='Y' or 'y':
        print ('Yes, I want to continue.')
        done = False
        break

    else:
        print('No. I want to quit now.')
        done = True
        sys.exit()

When I run this script on PyCharm, I get the outputs below:

Do you want to continue?
Type Y or y to continue; any other char to quit
Type here: y
y
Yes, I want to continue

Do you want to continue?
Type Y or y to continue; any other char to quit
Type here: n
n
Yes, I want to continue

Question: why is the "else" option ignored?

I tried so many variations like removing sys.exit() and so forth, but the loop still behaves the same way.

Aucun commentaire:

Enregistrer un commentaire