'I have a nested while loop and the father while loop will keep looping rather than ending with break
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
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fun_times |
| Solution 2 | Pyaive Oleg |
| Solution 3 | Peterrabbit |
