-2

I am an extreme beginner with python and I was wondering what is wrong with this program I set up to calculate tips

total = input("What is the bill total? ")

tperc = input("Would you like to give a 15% or 20% tip? ")

tip15 = total * .15
tip20 = total * .20

if tperc == "15":
    print("\nThat would be a $" + tip15 + "tip.")

if tperc == "15%":
    print("\nThat would be a $" + tip15 + "tip.")

if tperc == "20":
    print("\nThat would be a $" + tip20 + "tip.")

if tperc == "20%":
    print("\nThat would be a $" + tip20 + "tip.")

input("\nPress enter to exit.")

Thanks for the help

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
Rawr
  • 5
  • 2
  • 2
    What makes you think anything is wrong with it? – Kevin Sep 01 '15 at 14:35
  • 2
    Welcome to Stack Overflow. In order to help you, please tell us what you expect the program to do, what you have tried, what happens when you try it, and what you've attempted to do to fix it. – ScottMcGready Sep 01 '15 at 14:38
  • When I run it, I put in the bill total, and the tip %, then it immediately closes. "I set up to calculate tips" for say tipping your waiter? – Rawr Sep 01 '15 at 15:53

1 Answers1

3
total = input("What is the bill total? ")
#...
tip15 = total * .15

In Python 3.X, input returns a string. You can't multiply a string by a floating point number.

Convert total to a number before doing any arithmetic.

total = float(input("What is the bill total? "))

Or, ideally, use the Decimal type for money calculations, since floating point arithmetic tends to be not perfectly accurate.

from decimal import Decimal
total = Decimal(input("What is the bill total? "))
#...
tip15 = total * Decimal("0.15")

print("\nThat would be a $" + tip15 + "tip.")

This will also be a problem because you can't concatenate a string and a float/Decimal. Convert to a string or use string formatting. The latter may be preferable because you can round to two decimal places.

print("\nThat would be a $" + str(tip15) + " tip.")
#or
print("\nThat would be a ${:.2f} tip.".format(tip15))
Community
  • 1
  • 1
Kevin
  • 74,910
  • 12
  • 133
  • 166