Joe Johnson Joe Johnson - 9 months ago 36
Python Question

Using a for loop to calculate a meal price based on a range of tip percentages

I'm going back and forth and screwing myself up on this code. I have a formula to calculate total meal price.

def total_cost(price,tax,tip):

bill = price+(price*tax)+((price+(price*tax))*tip)
return bill

new_bill = total_cost(15,.08875,.18)


From there, how do I write a for loop that computes the total price of a meal, for different tipping rates, starting from 15% and ending at 25% (both inclusive) at 1% increments?

Answer Source

You just need to loop over the percentages:

for tip in range(15, 26, 1):  # the end point is exclusive with "range"
    cost = total_cost(15, 0.08875, tip/100.)  # in python3 you could use tip/100
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download