I'm new to python (PYTHON 3.4.2) and I'm trying to make a program that adds and divides to find the average or the mean of a user's input, but I can't figure out how to add the numbers I receive.
When I open the program at the command prompt it accepts the numbers I input and would print it also if I use a print function, but it will not sum the numbers up.
I receive this error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
My code is below:
#Take the user's input
numbers = input("Enter your numbers followed by commas: ")
sum([numbers])
Any help would be deeply appreciated.
First you need to convert elements of "numbers" to int, no need to strip the comma or whitespaces. This code is pretty straight forward and works fine.
input
takes a input as stringyou are telling user to give input saperated by comma, so you need to split the string with comma, then convert them to int then sum it
demo:
Simple: the list elements are stored as string :) So you have to convert all of them to int
Try the following code. It works for me. Actually
input()
tries to run the input as a Python expression. But theraw_input()
takes the input as string.input()
exists in Python 3.x.You can find more details hereOutput:
input
will give you string, and you are trying to concat string with int.