[英]how to get the sum of the numbers in a list in python?
N = []
stop = 1
while(stop != "0"):
number = input("Give a mark: ")
stop = raw_input("type 0 if you want to stop the program.")
N.append(number)
print (float(sum(N))) / (len(N))
这是我的代码。 我想知道我给程序的平均分数。 现在我认为这可以工作,但是会引发以下错误:
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
它与float和所有其他类型的数字相同,如果我不能使用sum(),该如何获得这些标记的和?
谢谢,我已经知道问题所在了,例如,我使用了2,0作为浮点数,但它必须是2.0,但是谢谢您的帮助。
首先,您读取的是字符串,而不是数字,因此必须首先将其转换。
N = []
stop = 1
while(stop != "0"):
number = float(input("Give a mark: "))
stop = raw_input("type 0 if you want to stop the program.")
N.append(number)
print sum(N) / len(N)
您得到的错误是因为sum
用0初始化一个integer类型的变量,然后开始将列表中的项目一个接一个地添加,因此尝试将字符串添加到and integer中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.