-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,682
  • 9
  • 34
  • 79

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
  • 7,941
  • 1
  • 21
  • 32
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,578
  • 1
  • 9
  • 12
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
  • 45,811
  • 27
  • 120
  • 136
hariK
  • 2,272
  • 11
  • 15