0

I am trying to create a tip calculator like every other newbie and of course I ran in to a problem. Most of the code works fine, but I can't figure out how to take user input for the tip and incorporate it into the grand total.

This page was a good resource and told me that I need to point python in the direction to interpret the input as math used in the solution. I wasn't able to translate it in my head into my code though.

Tip Calculator Function

# Tip Calculator
import random
bill = input("How much was your bill? ")
x = float(bill)
tip10 = x * .10
tip15 = x * .15

tip20 = x * .20

tip10 = float(tip10)
tip15 = float(tip15)
tip20 = float(tip20)

total = x + tip10
total = x + tip15
total = x + tip20
print(f"If you would like to leave a 10%, the tip amount will be ${tip10}.")
print(f"If you would like to leave a 15%, the tip amount will be ${tip15}.")
print(f"If you would like to leave a 20%, the tip amount will be ${tip20}.")
input("How much tip would you like to leave? ")
print(f"Your total is ${total:.2f}.)

When I run this it is only giving me the tip20 result after asking how much tip to leave, which I finally figured out is because it is the last line of the totals.

How can I incorporate the user input into the total for the last line of code?

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

1

Ask the user for input again, and then calculate total based on what they say.

tipAmount = input("How much tip would you like to leave? (10, 15, 20) ")
if tipAmount == "10":
    total = x + tip10
elif tipAmount == "15":
    total = x + tip15
elif tipAmount == "20":
    total = x + tip20
else:
    total = x
    print("You're not leaving a tip? You cheapskate.")
print(f"Your total is ${total:.2f}")

I'll leave the problem of making sure the user input is one of those three options as an exercise to the reader (if you get stuck, take a look at this answer).

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • 3
    While I realise it's just an example, the print message in the blanket else statement might not be cool if the user is feeling overly generous and enters something like 50. – aadibajpai Aug 08 '19 at 17:44
  • @aadibajpai Motivation to check the input and handle it dynamically! – Green Cloak Guy Aug 08 '19 at 17:49
1

There's no real need to hard-code each tip.

# Tip Calculator
import random
bill = input("How much was your bill? ")
x = float(bill)
example_tips = [10, 15, 20]

for tip in example_tips:
    print(f"If you would like to leave a {tip}%, the tip amount will be ${x*tip/100}}.")
choice = input("How much tip would you like to leave? ")
total = x*(1+float(choice)/100)
print(f"Your total is ${total:.2f}.)

Here, choice is intended to be a percentage as in example_tips.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

input("How much tip would you like to leave? ") doesn't output to a variable.

Also, total = x + tip20 is the only statement that has effect on total. What you'll want to do is to add the user input to the variable by changing this: input("How much tip would you like to leave? ") to this: total = x + float(input("How much tip would you like to leave? "))

Of course, this is if you want the tips you output to be a sort of suggestion for the user. If you want for the user to use your values, you're better off using Green Cloak Guy's answer.

Axiumin_
  • 2,107
  • 2
  • 15
  • 24