user2999565
user2999565

Reputation: 11

Invalid literal for int() with base 10: Python

I am trying to write a simple beginner program that acts as a tip calculator. Problem is, whenever I try to run the code, and input a decimal such as 25.2 for the cost of the meal, or "a", which is the value ive assigned to bind to the input, I get an error, and I cannot figure out why. Here is the code. Go easy on me, I'm new at this.

print("How much is your bill?")
a = int(input())
print("And the sales tax in your state?")
b = int(input())
print("How much do you wish to tip? (in percent)")
c = int(input())
d = b / 100
e = d * a
f = e + a
g = c / 100
h = g * f
i = h + f
print(i)

And here is my error message.

How much is your bill?
10.5
Traceback (most recent call last):
File "C:\Python33\Tip Calculator.py", line 3, in <module>
a = int(input())

Upvotes: 1

Views: 1236

Answers (2)

bnjmn
bnjmn

Reputation: 4584

If you expect the user to input a float, casting to an int will error.

Try casting to a float. Something like this

a = float(input())

Decimal is another option.

Upvotes: 1

Mariusz Jamro
Mariusz Jamro

Reputation: 31643

It's because 10.5 is not an int. Instead use:

a = float(input()) 

or preferably Decimal as you're dealing with monetary amounts you want to have fixed precision

from decimal import *

a = Decimal(input())​

Upvotes: 2

Related Questions