1

I am in a python programming class and I come from a health science background so a lot of this is very foreign to me. I've got an assignment to calculate various things from a restaurant, but one of them is the sum of the meals costs for a table. I have my program able to use the input number of customers at the table fore the range of the For loop, and if the user inputs something that is not a number the program will print a message that they did not input a valid number.

What my problem is, is that when the user inputs an invalid number, it still counts as part of the range/total sum of meals costs as a value of 0. So if I had 3 people at the table, all with meals costs of $10 and I enter the last 10 incorrectly, I am given a subtotal of $20.

print("Please enter the amount each customer's meal costs.")
sum_cost = 0
for value in range(num_meals):
    meal_cost = input("Enter the amount of one meal: ")
    try:
        meal_cost = float(meal_cost)
        sum_cost = sum_cost + meal_cost
    except ValueError:
        print("Please enter the cost of a meals as a number.")

This is inside of a larger while loop as well.

I don't necessarily need the range to increase, but if there is another way to configure the program to allow for another/extra meals cost to be imputed or if a while loop would be easier,

martineau
  • 119,623
  • 25
  • 170
  • 301
Rose Orion
  • 11
  • 1

0 Answers0