How can I use sum() function for a list in Python?

2019-01-24 10:50发布

I am doing my homework and it requirers me to use a sum () and len () functions to find the mean of an input number list, when I tried to use sum () to get the sum of the list, I got an error TypeError: unsupported operand type(s) for +: 'int' and 'str'. Following is my code:

numlist = input("Enter a list of number separated by commas: ")

numlist = numlist.split(",")

s = sum(numlist)
l = len(numlist)
m = float(s/l)
print("mean:",m)

7条回答
迷人小祖宗
2楼-- · 2019-01-24 11:31

Convert the string input to a list of float values. Here is the updated code.

numlist = list(map(int,input("Enter a list of number separated by commas: ").split(',')))
l = len(numlist)
s = sum(numlist)
print("mean :",s/l)
查看更多
登录 后发表回答