-4

I'm new to python programming and I want to know how to write a python program to get a some user input integers as a list and add all the items in it.This is the code I've written..

A=list(input("Enter the values of the list "))  
total=0  
for a in A:    
    total=total+a  
print(total)  

When I run this program it says,

Traceback (most recent call last):
  File "C:/Users/Jayaweera/Desktop/Python programming/51E5-sum all the items in a list.py", line 4, in <module>
    total=total+a
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Will
  • 4,299
  • 5
  • 32
  • 50
Oshadhi
  • 1
  • 1
  • 4
  • And I want a pony... – Willem Van Onsem Jan 24 '17 at 14:07
  • Cool idea OP. Why don't you do it though? – Ma0 Jan 24 '17 at 14:08
  • 1
    Mandatory readings: [ask], [MCVE], [How do I ask and answer homework questions?](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Łukasz Rogalski Jan 24 '17 at 14:09
  • 2
    Possible duplicate of [How can I read inputs as integers in Python?](http://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers-in-python) – OneCricketeer Jan 24 '17 at 14:10
  • I tried it several times but couldn't get the right answer – Oshadhi Jan 24 '17 at 14:10
  • Welcome to SO! We don't write code for you here, but we'd be happy to help you correct some code that isn't working. Edit your question to include the code you've tried and a description of what's going wrong, then you should be able to get some good answers. – Will Jan 24 '17 at 14:12
  • Second question: http://stackoverflow.com/questions/18730299/python-sum-function-with-list-parameter – OneCricketeer Jan 24 '17 at 14:12
  • `A=list(input("Enter the values of the list "))` will produce a list like this: `['1', ' ', '2', ' ', '3']`. Note that the elements are strings, so when you try to add them to `total` you get the error about adding strings to ints. you want: `A=input("Enter the values of the list ").split()` assuming your numbers are separated by a space. – Will Jan 24 '17 at 14:33
  • I changed it. But still it gives the error as, Traceback (most recent call last): File "C:/Users/Jayaweera/Desktop/Python programming/51E5-sum all the items in a list.py", line 4, in total=total+a TypeError: unsupported operand type(s) for +: 'int' and 'str' – Oshadhi Jan 24 '17 at 14:40
  • Whoops, since `split()` still keeps them as strings you will have to cast as an int to do the addition: `total = total + int(a)` – Will Jan 24 '17 at 16:23
  • The user input will be converted to a list of single character strings. What you want is numbers. You have to convert your input to the correct type. Something like this `A=[float(number) for number in input("Enter values: ").split()]` – Håken Lid Jan 25 '17 at 15:01
  • To understand exactly what is happening, instert a `print(A)` on line 2, so you can confirm that `A` is a list of numbers. – Håken Lid Jan 25 '17 at 15:04

1 Answers1

0

Try this modification, input will always return a string and you can't sum a string with an integer in python.

A=list(input("Enter the values of the list "))  
total=0  
for a in A:    
    total=total+int(a)  
print(total) 

Another thing is that first line, do you know how many values the list will have? If so why won't replace your first line with:

A = list()
totalElementsOfList = 100 # assuming it is for example 100 elements
for iteration in range(100):
    A.append(input("Value #" + str(iteration) + " :"))

for element in A:
    total += int(element)

print(total)
Gustavo Gomes
  • 317
  • 5
  • 13