Home > Enterprise >  I have a nested while loop and the father while loop will keep looping rather than ending with break
I have a nested while loop and the father while loop will keep looping rather than ending with break

Time:03-26

I made a nested while code and option 1 is the only one that works. It will break both while loops while options 2 and 3 only break their loops? I've tried copy-pasting the code for option 1 for the other 2 but alas no change.

#Volunteering
while True:
    print( Fname, " ", Lname, ", would you like to be a volunteer? [Y] [N]")
    VOLUNTEERyn = input()
    if VOLUNTEERyn == "Y":
            while True:
                print("Where would you like to work?")
                print("[1] Peir entrance gate")
                print("[2] Gift Shop")
                print("[3] Painting and decorating the Peir")
                print("Choose [1] or [2] or [3]")
                VOLUNTEERLOCATION = input()
                if VOLUNTEERLOCATION == "1":
                                          print("You now volunteer at the Peir entrance")
                                          break
            break            
            if VOLUNTEERLOCATION == "2":
                                        print("You now volunteer at the Gift Shop")
                                        break
            break                        
            if VOLUNTEERLOCATION == "3":
                                        print("You now volunteer as a Painter and Decorater")
                                        break
            break

I tried changing the indents to fix the problem but to no avail

CodePudding user response:

I am not sure while the "would you like to be a volunteer" runs in a infinite loop, but this works :

#!/usr/bin/python3

Fname = "Herp"
Lname = "McDerp"

while True:
    print( Fname, " ", Lname, ", would you like to be a volunteer? [Y] [N]")
    VOLUNTEERyn = input()
    if VOLUNTEERyn == "Y":
        while True:
            print("Where would you like to work?")
            print("[1] Peir entrance gate")
            print("[2] Gift Shop")
            print("[3] Painting and decorating the Peir")
            print("Choose [1] or [2] or [3]")
            VOLUNTEERLOCATION = input()
            if VOLUNTEERLOCATION == "1":
                print("You now volunteer at the Peir entrance")
                break
            if VOLUNTEERLOCATION == "2":
                print("You now volunteer at the Gift Shop")
                break
            if VOLUNTEERLOCATION == "3":
                print("You now volunteer as a Painter and Decorater")
                break
    break

CodePudding user response:

Yes, there are troubles with indents and "break" operators.

The code below works nice:

#Volunteering
Fname = "John"
Lname = "Doe"
while True:
    print(Fname, " ", Lname, ", would you like to be a volunteer? [Y] [N]")
    VOLUNTEERyn = input()
    if VOLUNTEERyn == "Y":
        while True:
            print("Where would you like to work?")
            print("[1] Peir entrance gate")
            print("[2] Gift Shop")
            print("[3] Painting and decorating the Peir")
            print("Choose [1] or [2] or [3]")
            VOLUNTEERLOCATION = input()
            if VOLUNTEERLOCATION == "1":
                print("You now volunteer at the Peir entrance")
                break          
            if VOLUNTEERLOCATION == "2":
                print("You now volunteer at the Gift Shop")
                break                     
            if VOLUNTEERLOCATION == "3":
                print("You now volunteer as a Painter and Decorater")
                break

CodePudding user response:

Fix indentation and use a boolean flag to break the first level loop.

# Volunteering
Fname = "Peter"
Lname = "Rabbit"

stop = False
while not stop:
    print( Fname, " ", Lname, ", would you like to be a volunteer? [Y] [N]")
    VOLUNTEERyn = input()
    if VOLUNTEERyn == "Y":
        while True:
            print("Where would you like to work?")
            print("[1] Peir entrance gate")
            print("[2] Gift Shop")
            print("[3] Painting and decorating the Peir")
            print("Choose [1] or [2] or [3]")
            VOLUNTEERLOCATION = input()

            if VOLUNTEERLOCATION == "1":
                print("You now volunteer at the Peir entrance")
                stop = True
                break

            elif VOLUNTEERLOCATION == "2":
                print("You now volunteer at the Gift Shop")
                stop = True
                break

            elif VOLUNTEERLOCATION == "3":
                print("You now volunteer as a Painter and Decorater")
                stop = True
                break
  • Related