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

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -