python - How to get a list of numbers as input and calculate the sum? -
mylist = int(raw_input('enter list: ')) total = 0 number in mylist: total += number print "the sum of numbers is:", total
correct way of doing same is:
separator = " " #define separator separating input integers. # 1,2,3,4 => separator = "," # 1, 2, 3, 4 => separator = ", " # 1 2 3 4 => separator = " " mylist = map(int, raw_input("enter list : ").split(separator)) print "the sum of numbers is: "+str(sum(mylist))
to find sum need convert space separated characters int
individually done above using map function.
Comments
Post a Comment