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



我正在尝试编写一个简单的初学者程序,充当提示计算器。问题是,每当我试图运行代码,并输入一个小数,如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:Python33Tip Calculator.py", line 3, in <module>
a = int(input())

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

a = float(input()) 

或者最好是十进制,因为你正在处理你想要固定精度的货币金额

from decimal import *
a = Decimal(input())​

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

试着铸造成浮子。像这样的

a = float(input())

Decimal是另一种选择。

相关内容

最新更新