Sum function prob TypeError: unsupported operand type(s) for +: 'int' and 'str'

后端 未结 5 1525
梦毁少年i
梦毁少年i 2020-12-03 13:07

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

5条回答
  •  被撕碎了的回忆
    2020-12-03 13:46

    Try the following code. It works for me. Actually input() tries to run the input as a Python expression. But the raw_input() takes the input as string. input() exists in Python 3.x.You can find more details here

    numbers = input("Enter your numbers followed by commas: ") ## takes numbers as input as expression
    print sum([i for i in numbers]) ## list comprehension to convert the numbers into invisible list. This is done because `sum()` runs only on iterable and list is iterable.
    

    Output:

    Enter your numbers followed by commas: 1,2,3,4
    10
    

提交回复
热议问题