我在这里有一个示例程序,当我运行它并选择一个选项但是我没有看到问题时返回以下错误。
TypeError:不支持的操作数类型 - :'str'和'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))
答案 0 :(得分:1)
如果您使用Python3,则input(question)
会返回字符串'1'
,'2'
等。您必须将其转换为数字int(input(question))
。