Python Type Error Unsupported Operand

Question:

I have a sample program here that is returning the following error when I run it and select an option but I don’t see the problem.

TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’

#Exception Handling

#If you haven't seen them before, you're not trying hard enough. What are they? Errors. Exceptions. Problems. Know what I'm talking about? I got it with this program:

#Code Example 1 - buggy program
def menu(list, question):
    for entry in list:
        print (1 + list.index(entry),)
        print (")" + entry)

    return input(question) -1

answer = menu(['A','B','C','D','E','F','H','I'],
'Which letter is your favourite?')

print ('You picked answer ' + (answer + 1))
Asked By: Xivilai

||

Answers:

If you use Python3 then input(question) returns string '1', '2', etc. You have to convert it into number int(input(question)).

Answered By: furas
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.