Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get the program to repeat itself after the total is printed? (Like a new person is ordering) I know I have to use a loop but don't know how

def main():
#this is the part that I want to restart after the total is given
        print("Welcome to static burger!")
        print("Please answer each question with y or n.")

   #The bill and food variables 
    bill = 0.00
    hb = 8.00
    cb = 6.00
    cs = 5.00
    fry = 2.00
    cdmts = 1.00
    
#The code adds up the user's order than prints his/her total.
    first = (input("Would you like a hamburger?"))
    if first == 'y':
        (input('with cheese?'))
        if first == 'y':
    
         bill += (hb + 2.00)
        else:
            bill += hb
    second = (input("Would you like a cheeseburger?"))
    if second == 'y':
        bill += cb
    third = (input("Would you like a chicken sandwich?"))
    if third == 'y':
        bill += cs
    fourth = (input("Would you like a fries?"))
    if fourth == 'y':
        bill += fry
    fifth = (input("Would you like condiments?"))
    if fifth == 'y':
        bill += cdmts
    else:
        bill = bill
    tip = 1.2
    total = bill * tip
    
    bill * 0.2
    #total of the users order
    print("Your total is $" "{:.2f}".format(total), "dollars")
    

#After this thankyou is printed, I want the code to restart as if a new person is making their order.
print("Thank you for your business!")
main()

#TLDR I just want to know which loop to use to make my keep restarting and making the user choose new items and get a new total.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add a while True:a statement from where you want your code to repeat.


    def main():
#this is the part that I want to restart after the total is given
    print("Welcome to static burger!")
    print("Please answer each question with y or n.")

   #The bill and food variables 
    bill = 0.00
    hb = 8.00
    cb = 6.00
    cs = 5.00
    fry = 2.00
    cdmts = 1.00
    
#The code adds up the user's order than prints his/her total.
    while True:
        first = (input("Would you like a hamburger?"))
        if first == 'y':
            (input('with cheese?'))
            if first == 'y':
        
             bill += (hb + 2.00)
            else:
                bill += hb
        second = (input("Would you like a cheeseburger?"))
        if second == 'y':
            bill += cb
        third = (input("Would you like a chicken sandwich?"))
        if third == 'y':
            bill += cs
        fourth = (input("Would you like a fries?"))
        if fourth == 'y':
            bill += fry
        fifth = (input("Would you like condiments?"))
        if fifth == 'y':
            bill += cdmts
        else:
            bill = bill
        tip = 1.2
        total = bill * tip
        
        bill * 0.2
        #total of the users order
        print("Your total is $" "{:.2f}".format(total), "dollars")
        print()

main()

To break the loop, you can add a condition and use the break statement.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading