如何获取数字列表作为输入并计算总和?

Eli*_*iza 2 python sum

mylist = int(raw_input('Enter your list: '))    
total = 0    
for number in mylist:    
    total += number    
print "The sum of the numbers is:", total
Run Code Online (Sandbox Code Playgroud)

Zda*_*daR 7

正确的做法是:

separator = " " #Define the separator by which you are separating the input integers.
# 1,2,3,4    => separator = ","
# 1, 2, 3, 4 => separator = ", "
# 1 2 3 4    => separator = " "

mylist = map(int, raw_input("Enter your list : ").split(separator)) 

print "The sum of numbers is: "+str(sum(mylist))
Run Code Online (Sandbox Code Playgroud)

为了找到你需要的空间分隔的字符转换成金额int 分别以上使用为已完成的地图功能.

  • 谢谢你解决这个问题.+1(主要用于向用户介绍`map`) (2认同)