3

I am new to Python and am having trouble wrapping my head around why this doesn't work.

number_string = input("Enter some numbers: ")

# Create List
number_list = [0]

# Create variable to use as accumulator
total = 0

# Use for loop to take single int from string and put in list
for num in number_string:
    number_list.append(num)

# Sum the list
for value in number_list:
    total += value

print(total)

Basically, I want a user to enter 123 for example and then get the sum of 1 and 2 and 3.

I am getting this error and do not know how to combat it.

Traceback (most recent call last):
  File "/Users/nathanlakes/Desktop/Q12.py", line 15, in <module>
    total += value
TypeError: unsupported operand type(s) for +=: 'int' and 'str'

I just can't find the answer to this in my textbook and don't understand why my second for loop won't iterate the list and accumulate the value to total.

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
Aaron
  • 151
  • 2
  • 2
  • 7

4 Answers4

11

You need to convert the strings to integers before you can add them.

Try changing this line:

number_list.append(num)

To this:

number_list.append(int(num))

Alternatively, a more Pythonic way of doing this would be to use the sum() function, and map() to convert each string in your initial list to an integer:

number_string = input("Enter some numbers: ")

print(sum(map(int, number_string)))

Be aware though, that if you input something like "123abc" your program will crash. If you are interested, look at handling exceptions, specifically a ValueError.

Pep_8_Guardiola
  • 5,002
  • 1
  • 24
  • 35
0

Change the line to:

total += int(value)

or

total = total + int(value)

P.S. Both of the code lines are equivalent.

Ajay
  • 130
  • 6
Sameer H. Ibra
  • 1,816
  • 2
  • 17
  • 25
0

Here is the official documentation about input in Python 3

 input([prompt])

If the prompt argument is present, it is written to standard output without a trailing    newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised. Example:

>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"

So when you do an input in the first line of your example you are basically getting strings.

Now you need to convert these string to int before summing up. So you would basically do:

total = total + int(value)

About Debugging:

When in similar situation, when you get errors like: unsupported operand type(s) for +=: 'int' and 'str', you can make use of the type() function.

Doing type(num) would tell you that it is a string. Obviously string and int cannot be added.

`

Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
0

I guess people have correctly pointed out the flaw in your code i.e the type conversion from string to int. However following is a more pythonic way of writing the same logic:

number_string = input("Enter some numbers: ")
print  sum(int(n) for n in number_string)

Here, we are using a generator, list comprehension and the library function sum.

>>> number_string = "123"
>>> sum(int(n) for n in number_string)
6
>>> 

EDIT:

number_string = input("Enter some numbers: ")
print  sum(map(int, number_string))
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Bhavish Agarwal
  • 663
  • 7
  • 13