2016-07-20 131 views
1
height_feet = int(input("Enter portion of height in feet ")) 
height_inch = int(input("Enter portion of heigh in inches")) 
height_in_inch = height_feet * 12 + height_inch 
height_in_cm = height_in_inch * 2.54 
print (height_in_cm) 

Enter portion of height in feet 6 
Enter portion of heigh in inches1.5 
Traceback (most recent call last): 
    File "Untitled.py", line 2, in <module> 
    height_inch = int(input("Enter portion of heigh in inches")) 
ValueError: invalid literal for int() with base 10: '1.5' 
>>> 

我的品牌新的Python,我不明白为什么它显示此错误,当我试图通过一些带有小数点ValueError异常:无效的字面INT()基数为10:

+2

您正在寻找'float'而不是'int'(用于英寸测量)。整数是一个可以不带分数的数字。没有一个分数就不能写1.5。 – Alex

+0

您不能将非整数的字符串转换为整数。 1.5不是一个整数,所以会引发该错误。尝试使用'float'代替 –

+2

@PeterWang Python没有*强制转换*。调用'int'将*解析字符串并获得一个'int'值。 Casting是*静态*类型语言中使用的操作,您基本上告诉编译器将该值视为其他类型。有时它需要对值进行小的修改(例如,添加填充'0'),但肯定不像解析十进制文字。 – Bakuriu

回答

0

倍增这应该是float类型,而不是int类型。

0

在Python和其他地方,整数只有整数数字。 1.5,1.2,pi,带小数的任何东西都不是int。你想要float来描述它们。它是浮点数的简称。

相关问题