Question:
Error in my python program, can someone please tell me what I did wrong?
?
2013-09-14 07:12:22 UTC
Okay, so I wrote this program on a computer at my university, and it was working fine, with the exception of the last line in the code. I printed out the code at my college and retyped in my home python program, and now that I've brought it home and started using it on my computer, it's not working right. The code is:

lines = list()

x = int(input("How many numbers do you want to sort and average?"))

for i in range(x):
..........line = input("What's your number?")
..........lines.append(line)

print("Your numbers are:")

for line in lines:
..........print(line)

print("Your numbers in sorted order are:")
lines.sort()
print(lines)

print("The average of your numbers is:")

print(sum(lines), '/', len(lines))

input("Press the enter key to exit.")

The program works up to the following line:

print(sum(lines), '/', len(lines))

and then it prints the following error message:

Traceback (most recent call last):
File "C:/Users/Leandra/Desktop/Homework (Unknown Number).py", line 26, in
print(sum(lines), '/', len(lines))
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Can someone please tell me what I'm doing wrong? I've looked over the program, and I don't see a reason for this not to work right.
Three answers:
Russel
2013-09-14 13:54:37 UTC
I imagine Duncan didn't get the error because he tried it in Python 2. Python 2 has raw_input() and input() where raw_input() always returns a string and input() is the equivalent of eval(raw_input()). In Python 3 which is what you're using, there is only input() which always returns a string just like the Python 2 raw_input().



So this explains the fact that the lines variable is actually a list of strings.



Now the way that sum() works in both Python 2 and 3 as far as I know is it takes sequence argument (which can be any iterable) and an optional start argument. When no start argument is provided then the default start is the int 0.



So sum([1, 2, 3]) is the same as sum([1, 2, 3], 0). The following is not actually the code for sum() but it explains how it works and the origin of the error message.



def sum(sequence, start=0):

....for x in sequence:

........start += x

....return start



Now if you say sum(['1', '2', '3']) it's going to start from the int 0 (the default start argument) and try to add that with a string and the following error occurs.



>>> 1 + '2'



Traceback (most recent call last):

File "", line 1, in

1 + '2'

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

>>>



To fix it simply change this



lines.append(line)



to this



lines.append(int(line))
?
2013-09-14 14:49:38 UTC
Mystery. Your code works fine here. Unable to replicate your error at all.





As you are not using the + operand the error message makes no sense.





Try saving the code as a fresh file (with another name) and see if that runs.
G P
2013-09-14 14:20:07 UTC
Well, i think the first error is requiring the user to supply the number of items instead of having the program. Figure it out.

but in relation to your error message, It looks like you're reading a string into line


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...