我只需要将三个数字相加并计算平均值
import sys
sums=0.0
k=3
for w in range(k):
sums = sums + input("Pleas input number " + str(w+1) + " ")
print("the media is " + str(sums/k) + " and the Sum is " + str(sums))
和错误:
Pleas input number 1 1
Traceback (most recent call last):
File "/home/user/Python/sec001.py", line 5, in <module>
sums = sums + input("Pleas input number " + str(w+1) + " ");
TypeError: unsupported operand type(s) for +: 'float' and 'str'
原文由 userDepth 发布,翻译遵循 CC BY-SA 4.0 许可协议
input()
函数返回一个字符串(str),Python 不会自动将其转换为浮点数/整数。您需要做的就是转换它。如果你想做得更好,你可以使用 try/except 来处理无效输入。此外,不需要
import sys
,您应该避免使用分号。