在施法时我犯了什么错误?

时间:2015-07-29 21:43:42

标签: python

我在尝试计算calc

值的行中出现错误
num1 = input("Enter first number:")
num2 = input("Enter second number:")
num3 = input("Enter third number:")

print("Output:")
calc = float((num1 + num2 + num3)/ 2)
print("{0}+{1}+{2})/2 is:{3}".format(num1,num2,num3,calc))

这就是我感谢你的帮助......

num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))
num3 = float(input("Enter third number:"))

print("Output:")
calc = ((num1 + num2 + num3)/ 2)
print("{0}+{1}+{2})/2 is:{3}".format(num1,num2,num3,calc))

1 个答案:

答案 0 :(得分:6)

输入返回字符串!所以,将这三个数字转换为int!正如评论所说,更好地直接在浮动中进行转换。

num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))
num3 = float(input("Enter third number:"))

print("Output:")
calc = (num1 + num2 + num3)/ 2
print("{0}+{1}+{2})/2 is:{3}".format(num1,num2,num3,calc))

>>> 
Result:
Enter first number:12
Enter second number:2
Enter third number:5
Output:
12+2+5)/2 is:9.5
>>>