0

It seems I can't get my nestled while loop to stop repeating the output print('Y/N only accepted answers.'). The first while loop I have works as intended, whereas if I enter anything other than Y/y/N/n, it tells me once that Y/N are the only accepted answers, then repeats the question a single time.

def initalSequence():
    print('*WELCOME*')
    trueVar1 = True
    while trueVar1:
        response1 = input('Do you like pizza? Y/N: ')
        if response1 == 'Y' or response1 == 'y' or response1 == 'N' or response1 == 'n':
            response2 = input('Are you sure about that? (Y/N): ')
            trueVar1 = False
            trueVar2 = True
            while trueVar2:
                if response2 == 'Y' or response2 == 'y' or response2 == 'N' or response2 == 'n':
                    print(countDown())
                    trueVar2 = False
                    break
                else:
                    print('Y/N only accepted answers.')
                    continue
        else:
            print('Y/N only accepted answers.')
            continue

print(initalSequence())

I simply want the nestled while loop to do exactly what the initial while loop is doing. I could understand utilizing a try and except if I was asking for integers, but I don't know of how I could utilize that with a string.

GopnikJr
  • 1
  • 2
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – MattDMo Aug 01 '22 at 00:22

1 Answers1

0

TLDR: Move line response2 = input('Are you sure about that? (Y/N): ') inside the nestled while loop.

I simply want the nestled while loop to do exactly what the initial while loop is doing.

What the outer while loop does.

trueVar1 = True
while trueVar1:
    response1 = input('Do you like pizza? Y/N: ')
    if response1 == 'Y' or response1 == 'y' or response1 == 'N' or response1 == 'n':
        trueVar1 = False
        # Do something. Once done, exit loop
    else:
        print('Y/N only accepted answers.')
        continue

So, the inner while loop should be

trueVar2 = True
while trueVar2:
    response2 = input('Are you sure about that? (Y/N): ')
    if response2 == 'Y' or response2 == 'y' or response2 == 'N' or response2 == 'n':
        trueVar2 = False
        # Do something, once done, exit loop
    else:
        print('Y/N only accepted answers.')
        continue

This should work.

Why does my nestled while loop keep repeating?

Because response2 input is acquired outside the inner while loop, and if its value is anything different from Y or y or N or n, it will cause a infinite loop, very like the example below.

while True:
    if False:
        # nothing inside this if will be executed,
    else:
        print('Y/N only accepted answers.')
        continue

The solution, simply move response2 = input('Are you sure about that? (Y/N): ') inside the nestled while loop.

AlberNovo
  • 126
  • 1
  • 5