Question

I have just started to use python. The first program I made was a tip calculator and I have made three versions to rewrite and to add more. The next part of code I want to write is a loop that prompts a yes or no question. When 'yes' I want the program to loop back to the very start, when 'no' I want the program to exit, and when there is an invalid command I want the program to print('Invalid command.') and continue to wait for a 'yes' or 'no'. Here is the code:

    print('Good evening sir, I am Tippos! Please, tell me the price of your meal and how much you would like to tip and I\'ll do the math for you!')
bill_amt = input('First sir, what was the price of your meal?(Please do not use signs such as "$"):')

tax = 1.13
x = float(bill_amt)
tip_amt = input('And how much would you like to tip? 10, 15, or maybe 20% Go on, any number sir!(Please do not use signs such as "%"):')
y = float(tip_amt)

tip_amt = x * (y / 100)
print('Your tip will cost you an extra' ,tip_amt, 'dollars.')
total_amt = (x + y) * tax
print('Your total cost will be' ,total_amt, 'dollars, sir.')

How do I add a loop that will restart the program at a certain input? Thanks! -Pottsy

Was it helpful?

Solution

a good way is one such as the following:

done = False
while not done:
    print('Good evening sir, I am Tippos! Please, tell me the price of your meal and how much you would like to tip and I\'ll do the math for you!')
    bill_amt = input('First sir, what was the price of your meal?(Please do not use signs such as "$"):')

    tax = 1.13
    x = float(bill_amt)
    tip_amt = input('And how much would you like to tip? 10, 15, or maybe 20% Go on, any number sir!(Please do not use signs such as "%"):')
    y = float(tip_amt)

    tip_amt = x * (y / 100)
    print('Your tip will cost you an extra' ,tip_amt, 'dollars.')
    total_amt = (x + y) * tax
    print('Your total cost will be' ,total_amt, 'dollars, sir.')
    if 'yes' != input('Do you want to start over?').lower():
        done = True

where a variable done is set up in a while loop, then when you ask the question to start over, if the answer is not exactly yes (in any case), then you stop your algorithm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top