-6

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 ()
GLHF
  • 3,835
  • 10
  • 38
  • 83

3 Answers3

0

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
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
0

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).

Tony
  • 1,645
  • 1
  • 10
  • 13
0
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
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
hariK
  • 2,722
  • 13
  • 18