Python Forum
Struggling to exit this while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Struggling to exit this while loop
#1
Hello, I am a student (GCSE level) and I am writing this program which states the type of discount you require based on your age. I am confused about the results of exiting the while loops in the following program;

discount = "You are aged between 13 and 15", ("Discount - 30%"), "You are aged between 16 and 17", (
"Discount - 20%"), "You are aged 50 and over ", ("Discount - 40%")

age13_15, discount30, age16_17, discount20, age_50, discount40 = discount
menu = ""
exit_while = ""

while exit_while != "exit":
age = int(input("How old are you: "))
name = input("What is your name: ")
exit_while = ""

if age < 13:
print("You are too young to purchase products/licenses to have accesss to the swimming centre. Please try again in {} years".format(13 - age))
elif 13 <= age <= 15:
print("Hi")
print("{0} \t {1}".format(age13_15, discount30))
elif 16 <= age <= 17:
print("Hi")
print("{0} \t {1}".format(age16_17, discount20))
elif age >= 50:
print("Hi")
print("{0} \t {1}".format(age_50, discount40))
else:
print("You are aged over 17 and under 50 years old. Please try again in {} years".format(
50 - age))

while exit_while != "exit" or "again":
menu = input("""Do you want to try the program again; Enter "quit" to exit,
or enter "again" to try the program again: """)
menu = menu.upper()
if menu == "QUIT":
print("Entry entered")
exit_while = "exit"
elif menu == "AGAIN":
print("Entry entered")
exit_while = "again"
else:
print("invalid entry")


If I was to enter "QUIT", the program should totally stop. If I was to enter "AGAIN", we should exit the last while (embedded while), but should not exit the main while loop.

The result when entering "QUIT" or "AGAIN" is "Entry entered
Do you want to try the program again; Enter "quit" to exit,
or enter "again" to try the program again:"

I am a beginner in python but I am getting better at it. It may be a simple error that I cannot see. Whoever replies, thanks in advance :)
Reply
#2
Hello, fatherted99.

First of all, although one could still read your code, please add the corresponding indentation to each block. This is how it will actually run and is more intuitive to read.

Without reading your code very carefully—because of the indentation—you don't really need the triple double quotes to use double quotes in a string, just use single quotes when you'll use one inside, unless you'll use both inside, in which case you can just escape them with \.
Reply
#3
discount = "You are aged between 13 and 15", ("Discount - 30%"), "You are aged between 16 and 17", (
    "Discount - 20%"), "You are aged 50 and over ", ("Discount - 40%")

age13_15, discount30, age16_17, discount20, age_50, discount40 = discount
menu = ""
exit_while = ""

while exit_while != "exit":
    age = int(input("How old are you: "))
    name = input("What is your name: ")
    exit_while = ""

    if age < 13:
        print("You are too young to purchase products/licenses to have accesss to the swimming centre. Please try again in {} years".format(13 - age))
    elif 13 <= age <= 15:
        print("Hi")
        print("{0} \t {1}".format(age13_15, discount30))
    elif 16 <= age <= 17:
        print("Hi")
        print("{0} \t {1}".format(age16_17, discount20))
    elif age >= 50:
        print("Hi")
        print("{0} \t {1}".format(age_50, discount40))
    else:
        print("You are aged over 17 and under 50 years old. Please try again in {} years".format(
            50 - age))

    while exit_while != "exit" or "again":
        menu = input("Do you want to try the program again; Enter \"quit\" to exit, or enter \"again\" to try the program again.")
        menu = menu.upper()
        if menu == "QUIT":
            print("Entry entered")
            exit_while = "exit"
        elif menu == "AGAIN":
            print("Entry entered")
            exit_while = "again"
        else:
            print("invalid entry")
Sorry about my indentation. Here is the indented code.
Reply
#4
Okay, I can read the code better now.

I see that you are binding your loop variable to new values when you want to exit the loops and that you actually want your loops to run infinitely unless the values "exit" or "again" are entered. Now, have a look at the keyword break, that will help you exit your loops whenever you want. You can just use while True: if you want them to run forever unless those words are entered.

I think the code can be improved in other ways but you'll need to take care of this first.
Reply
#5
I think a 'break' is missing somewhere. Compare with this
want_to_stay = True

while want_to_stay:
    while True:
        reply = input('What do you want to do (exit or again)? ')
        if reply == 'exit':
            want_to_stay = False
        elif reply != 'again':
            print('Error: invalid reply, please try again')
            continue
        break
Reply
#6
This has completely fixed my program. Thanks.
while True:
        menu = input(
            "Do you want to try the program again; Enter \"quit\" to exit, or enter \"again\" to try the program again.")
        menu = menu.upper()
        if menu == "QUIT":
            print("Entry entered")
            exit_while = "exit"
            break
        elif menu == "AGAIN":
            print("Entry entered")
            break
        else:
            print("invalid entry")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,764 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Struggling with Juggling JSON Data SamWatt 7 1,820 May-09-2022, 02:49 AM
Last Post: snippsat
  Syntax errors: Struggling to setup enviroment and load packages AH56 5 2,724 Jun-30-2021, 01:01 PM
Last Post: AH56
  While loop doesn't exit KenHorse 3 1,967 Jun-20-2021, 11:05 PM
Last Post: deanhystad
  Struggling for the past hour to define function and call it back godlyredwall 2 2,158 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Exit Function - loop Tetsuo30 2 2,025 Sep-17-2020, 09:58 AM
Last Post: Tetsuo30
  struggling with != statements CallumRoberts2004 2 1,509 Aug-18-2020, 03:01 PM
Last Post: GOTO10
  I’m Flat out struggling to understand list indexes gr3yali3n 7 2,854 Jul-20-2020, 07:18 PM
Last Post: princetonits
  Struggling with nested list gr3yali3n 3 2,257 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Struggling with several while loops nsadams87xx 1 1,791 Nov-25-2019, 02:12 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020