1
N = []
stop = 1

while(stop != "0"):
    number = input("Give a mark: ")
    stop = raw_input("type 0 if you want to stop the program.")
    N.append(number)

print (float(sum(N))) / (len(N))

this is my code. I want to know the average of the marks I give the program. Now I thought this would work, but it throws the following error:

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

it does the same with float and all other types of numbers, how am I sopposed to get the sum of those marks if I cant use sum()?

5
  • Your problem is too many brackets. Just use print 1.0 * sum(N) / len(N) May 4, 2015 at 15:20
  • N = [1,2,3]; print (float(sum(N))) / (len(N)) works OK, cannot reproduce. May 4, 2015 at 15:20
  • The output of the function input is a string which you store into a List. First cast this to an int, and then continue.
    – RvdK
    May 4, 2015 at 15:20
  • Well how would you calculate the sum of a list of numbers without a computer or calculator?
    – jonrsharpe
    May 4, 2015 at 15:20
  • Somewhere you have a trailing comma May 4, 2015 at 15:33

2 Answers 2

2

thank you, I already know what the problem was, I used for example 2,0 for a float, but it had to be 2.0, but thank you for your help.

1
  • You should also use float(raw_input()) instead if using input. May 4, 2015 at 15:35
0

First off, what you read are string, not numbers, so you must convert them first.

N = []
stop = 1

while(stop != "0"):
    number = float(input("Give a mark: "))
    stop = raw_input("type 0 if you want to stop the program.")
    N.append(number)

print sum(N) / len(N)

The error you got is because sum initializes a variable of type integer with 0 and then starts adding the items in the list one by one, and hence it tries to add a string to and integer.

1
  • 1
    That actually would make no difference, while not a good way to do it input would return a float in python 2 not a string, the error has also got noting to do with sum. May 4, 2015 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.