I want to make a nested loop that makes user enter a number that does not make the input variable have a negative output. How with changing variable?

Question:

This is Python in Visual Studio Code. I’m making a program for school that essentially is an robotic restaurant waitress. I have to do it the way the assignment outline says which is why it might be a little weird. Then I have to personalize it and make it creative. It asks how much the price of an adult meal and a child meal. Then it asks how many children and adults there are. Then it asks what the sales tax rate is. The assignment only needs me to be able to calculate all that and get the subtotal and total and then give change. I’ve already done that and I’m making it fancy. I made a loop that asks if they have a lucky draw coupon that generates a random monetary value between $1.00-$5.00. Then If they say yes it will bring them to the first part of the loop that gives them the total with the coupon and then asks for payment. If they say no, then it just asks them for the payment.

I want to go the extra mile and put a loop in a loop, which I think is called a nested loop (This is my first week of Python.) I want it to recognize if the amount they pay is less then the total, then ask for the user to try again until they put an amount more than the total. Pretty much I just want it to know that the payment isn’t enough.

My problem is I’ve never used nested loops and this is the first time I’ve even used a loop period. I also don’t know how to make a loop based on a variable that’s not constant because it’s based on what the user inputs when prompted. I could do it if I could put the parameters in a fixed range, but the range will always be different based on what they enter in the beginning.

How do I make a nested loop recognize a variable that is never the same?
How do I make it recognize the payment is not enough to cover the total?

This is the loop I have. And I want to put two nested loops in the loop. One for if and one for elif so that it works for both "options".
Here is my snippet:

while True:
    coup = input("Do you have a lucky draw coupon? 1 for YES 2 for NO: ")
    if coup =="1":
        #calling the random function and also rounding it so that it comes out as two decimal places
        coupon = round(random.uniform(1.00,5.00),2)
        print()
        print("---------------------")
        #tells the user how much they save and the :.2f is making sure it has two decimal places
        print(f"You will save ${coupon:.2f}!")
        print("---------------------")
        print()
        newtotal = total - coupon
        print(f"Your new total is: ${newtotal:.2f}")
        print()
        payment = float(input("Please enter your payment amount: "))
        change2 = payment - total + coupon
        change2 = round(change2, 2)
        print(f"Change: ${change2:.2f}")
        print()
        print("---------------------------------------------------------------------")
        print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
        print("---------------------------------------------------------------------")
        print()
        #break stops the loop from looping again
        break
    elif coup == "2":
        print()
        print("That's ok! Maybe next time!")
        print()
        payment = float(input("Please enter your payment amount: "))
        change = payment - total
        change = round(change, 2)
        print(f"Change: ${change:.2f}")
        print()
        print("---------------------------------------------------------------------")
        print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
        print("---------------------------------------------------------------------")
        print()
        break
    else:
        print()
        print("Invalid response. Please type 1 for YES and 2 for NO: ")

Any suggestions? I’ve searched all over Stackoverflow and google and cannot find anything specific to my situation.

Asked By: Erin McKee

||

Answers:

Your approach is good for implementing this. Looping while asking for payment and validating that it is correct before allowing the loop to break will do the job. For your first if block, this is how that would look:

while True:
    coup = input("Do you have a lucky draw coupon? 1 for YES 2 for NO: ")
    if coup =="1":
        #calling the random function and also rounding it so that it comes out as two decimal places
        coupon = round(random.uniform(1.00,5.00),2)
        print()
        print("---------------------")
        #tells the user how much they save and the :.2f is making sure it has two decimal places
        print(f"You will save ${coupon:.2f}!")
        print("---------------------")
        print()
        newtotal = total - coupon
        print(f"Your new total is: ${newtotal:.2f}")
        print()
        
        #Ask for the payment inside of the new loop
        while True:
             payment = float(input("Please enter your payment amount: "))
             if payment - total + coupon > 0:
                 print("That's not enough money to pay your bill. Try again")
             else:
                 break

        change2 = payment - total + coupon
        change2 = round(change2, 2)
        print(f"Change: ${change2:.2f}")
        print()
        print("---------------------------------------------------------------------")
        print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
        print("---------------------------------------------------------------------")
        print()
        #break stops the loop from looping again
        break
    elif coup == "2":
        print()
        print("That's ok! Maybe next time!")
        print()
        payment = float(input("Please enter your payment amount: "))
        change = payment - total
        change = round(change, 2)
        print(f"Change: ${change:.2f}")
        print()
        print("---------------------------------------------------------------------")
        print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
        print("---------------------------------------------------------------------")
        print()
        break
    else:
        print()
        print("Invalid response. Please type 1 for YES and 2 for NO: ")
Answered By: JNevill