I am having an issue with getting a inputted list to spit out the sum can you help. Sorry I'm a newbie
def adding ():
a = input("Type the numbers you want to add here")
b = sum(a)
print(b)
adding ()
I am having an issue with getting a inputted list to spit out the sum can you help. Sorry I'm a newbie
def adding ():
a = input("Type the numbers you want to add here")
b = sum(a)
print(b)
adding ()
I guess you are looking for this:
>>> a = input("Type the numbers you want to add here: ")
Type the numbers you want to add here: 1 2 3 4 5
>>> b = sum(map(int,a.split()))
>>> b
15
If you are using Python 3, your input function will return a string (a list of characters). You will need to split this up and convert the bits into numbers to add them. You can do it like this:
sum([int(x) for x in a.split()])
.
The square brackets are what is known as a list comprehension (which you can Google if you want to know more).
If you are using Python 2, you should use raw_input("Type numbers...")
instead of input
, and then split/convert (or do what @BhargavRao suggested).
numbers = raw_input("Enter the Numbers : ")
number_list = map(int, numbers.split())
print sum(number_list)
Output
Enter the Numbers : 8 3 9 0
20