| Start |
|---|
| TypeErrors These exceptions are caused when the type of some object should be different TypeError: [definition/method] takes ? positional arguments but ? was givenA function or method was called with more (or less) arguments than the ones it can accept. ExampleIf more arguments are given: If less arguments are given: Note: if you want use an unknown number of arguments, you can use TypeError: unsupported operand type(s) for [operand]: '???' and '???'Some types cannot be operated together, depending on the operand. ExampleFor example: Some types (eg: Or they may not be even used for anything: But you can for example add a TypeError: '???' object is not iterable/subscriptable:For an object to be iterable it can take sequential indexes starting from zero until the indexes are no longer valid and a ExampleHere we are saying that This is a more discrete version: In this example TypeError: '???' object is not callableYou are defining a variable and calling it later (like what you do with a function or method) Example |
| NameError: name '???' is not defined Is raised when you tried to use a variable, method or function that is not initialized (at least not before). In other words, it is raised when a requested local or global name is not found. It's possible that you misspelt the name of the object or forgot to It's simply not defined nowhere in the codeIt's possible that you forgot to initialize it, specially if it is a constant Maybe it's defined later: Or it wasn't |
| IndentationErrors (or indentation SyntaxErrors) In most other languages indentation is not compulsory, but in Python (and other languages: early versions of FORTRAN, Makefiles, Whitespace (esoteric language), etc.) that is not the case, what can be confusing if you come from another language, if you were copying code from an example to your own, or simply if you are new. IndentationError/SyntaxError: unexpected indentThis exception is raised when the indentation level increases with no reason. ExampleThere is no reason to increase the level here: Here there are two errors: the last one and that the indentation does not match any indentation level. However just one is shown: IndentationError/SyntaxError: unindent does not match any outer indentation levelAppears you didn't unindent completely. ExampleIndentationError: expected an indented blockAfter a colon (and then a new line) the indentation level has to increase. This error is raised when that didn't happen. ExampleNote: Use the keyword IndentationError: inconsistent use of tabs and spaces in indentationExampleHow to avoid this errorDon't use tabs. It is discouraged by
See this (opens new window) question if you want to learn more. |
Other Errors AssertErrorThe or: It's equivalent to this: Assertions can include an optional message, and you can disable them when you're done debugging. Note: the built-in variable debug is True under normal circumstances, False when optimization is requested (command line option -O). Assignments to debug are illegal. The value for the built-in variable is determined when the interpreter starts. KeyboardInterruptError raised when the user presses the interrupt key, normally Ctrl + C or del. ZeroDivisionErrorYou tried to calculate It raises |
| Syntax Error on good code The gross majority of the time a SyntaxError which points to an uninteresting line means there is an issue on the line before it (in this example, it's a missing parenthesis): Returns The most common reason for this issue is mismatched parentheses/brackets, as the example shows. There is one major caveat for print statements in Python 3: Because the |
Read Article
https://develop.horje.com/learn/1434/reference