Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to create a menu in python but loop won't exit

Here is my code:

item=0
while True:
    menu=input (""" ----- Heart Attacks On A Bun ----------
          1.    The Big MOO Combo . . . . 5.99
          2.    Big MOO . . . . . . . . . 3.99
          3.    Spring Surprise . . . . . 1.99
          4.    Fries . . . . . . . . . . 1.29
          5.    Pop . . . . . . . . . . . 1.19
          6.    Exit
          ________________________________________

          What would you like? one for combo, two for Big MOO etc.
          """)
    if item=="one":
        item+=5.99
    elif item=="two":
        item+=3.99
    elif item=="three":
        item+=1.99
    elif item=="Four":
        item+=1.29
    elif item=="Five":
        item+=1.19
    elif item=="six":
        break
        print ("Your total is", item,"dollars")

How come when I type in six my loop doesn't exit and print the total? Also I am a beginner this is for a highschool course

like image 351
xHoogle007 Avatar asked Aug 02 '26 03:08

xHoogle007


2 Answers

There are several problems with the code as you wrote it.

  1. menu needs to be checked for input, not item(e.i. if item==should be if menu==)
  2. item=="one" needs to be changed to menu == 1(Same for the rest of the comparisons) unless you expect users to type in "one" rather than pressing 1
  3. break needs to be moved to the line after your final print statement, as it is breaking before you can print anything.

Nitpick: print ("Your total is", item,"dollars") prints a tuple, which doesn't look very nice. print "Your total is $" + str(item) looks much more professional.

Fixed:

item=0
while True:
    menu = int(input (""" ----- Heart Attacks On A Bun ----------
          1.    The Big MOO Combo . . . . 5.99
          2.    Big MOO . . . . . . . . . 3.99
          3.    Spring Surprise . . . . . 1.99
          4.    Fries . . . . . . . . . . 1.29
          5.    Pop . . . . . . . . . . . 1.19
          6.    Exit
          ________________________________________

          What would you like? one for combo, two for Big MOO etc.
          """))

    if menu == 1:
        item+=5.99
    elif menu == 2:
        item+=3.99
    elif menu == 3:
        item+=1.99
    elif menu == 4:
        item+=1.29
    elif menu == 5:
        item+=1.19
    elif menu == 6:
        print("Your total is $" + str(item))
        break
like image 181
David Greydanus Avatar answered Aug 03 '26 17:08

David Greydanus


item=0    
while True:

    menu =input(""" ----- Heart Attacks On A Bun ----------

          1.    The Big MOO Combo . . . . 5.99
          2.    Big MOO . . . . . . . . . 3.99
          3.    Spring Surprise . . . . . 1.99
          4.    Fries . . . . . . . . . . 1.29
          5.    Pop . . . . . . . . . . . 1.19
          6.    Exit
          ________________________________________

          What would you like? one for combo, two for Big MOO etc.
          """)

    if menu == str(1):
        item+=(5.99)
    elif menu == str(2):
        item+=3.99
    elif menu == str(3):
        item+=1.99
    elif menu == str(4):
        item+=1.29
    elif menu == str(5):
        item+=1.19
    elif menu == str(6):
        print ("Your total is", item,"dollars")
        break

The input could be changed to a str and your break command came before your print command.

Or it could be done just as David explained before me (I actually would prefer his way, as it requires the int input, as opposed to my conversion to str.)

like image 23
J. Saw Avatar answered Aug 03 '26 16:08

J. Saw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!