int()的基数为10的文字无效:Python

时间:2013-11-16 15:08:16

标签: python python-3.3

我正在尝试编写一个简单的初学者程序,作为小费计算器。问题是,每当我尝试运行代码,并输入一个小数,如25.2作为用餐成本,或“a”,这是我赋予绑定到输入的值,我得到一个错误,我不能找出原因。这是代码。对我很轻松,我是新手。

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)

这是我的错误信息。

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())

2 个答案:

答案 0 :(得分:2)

这是因为10.5不是int。而是使用:

a = float(input()) 

或者最好是Decimal,因为您正在处理您想要具有固定精度的货币金额

from decimal import *

a = Decimal(input())​

答案 1 :(得分:1)

如果您希望用户输入float,则转换为int会出错。

尝试施放到浮子上。像这样的东西

a = float(input())

Decimal是另一种选择。