A Beginner's Python Tutorial - Exception Handling - Wikibooks, Open Books For An Open World
A Beginner's Python Tutorial - Exception Handling - Wikibooks, Open Books For An Open World
Tutorial/Exception
Handling
< A Beginner's Python Tutorial
return
raw_input(question) - 1
File
"/home/steven/errortest.py",
line 10, in -toplevel- tells us
a couple of things. File
"/home/steven/errortest.py" tells us
which file the error occurred in. This is
useful if you use lots of modules that
refer to each other. line 10, in -toplevel-
tells us that it is in line # 10 of the file,
and in the top level (that is, no
indentation).
answer =
menu(['A','B','C','D','E','F'
,'H','I'],'Which letter is
your favourite? ') duplicates the
code where the error is.
Since this line calls a function, the next
two lines describe where in the function
the error occurred.
TypeError: unsupported
operand type(s) for -: 'str'
and 'int' tells you the error. In this
case, it is a 'TypeError', where you tried
to subtract incompatible variables.
answer =
menu(['A','B','C','D','E','
F','H','I'],\
'Which letter is your
favourite? ')
return input(question) - 1
Bug fixed!
Exceptions - Limitations of
the Code
Endless Errors