An error message is a clue, not a punishment. This note shows how to read it from the bottom, what the common error names mean, and what to do when the program runs but gives the wrong answer.
Every programmer sees errors, every day. The skill is not to avoid them. The skill is to read them.
| Error name | What it usually means |
|---|---|
| SyntaxError | Something is missing or misspelt in the shape of the code: a colon, a bracket, a quote. |
| NameError | A name was used before it was made, or it is spelt differently. |
| TypeError | Two kinds of value that do not go together, such as "5" + 3. |
| ValueError | The kind of value is right, but the value is wrong, such as int("abc"). |
| IndexError | A position that is not in the list. |
| ZeroDivisionError | A division by zero. |
This program has a mistake on purpose. Run it and read the last line of the message.
Sometimes the program runs without an error, but the answer is wrong. This is a logic error. Python cannot find it for you, because it does what you wrote, not what you meant. To find it, print the values in the middle of the program and compare them with what you expect.
Write your answers in full sentences. Try each one before you read the pointers.
print("Total: " + 5). (b) print(nam) when the variable is called name. (c) a for i in range(3) line with no colon at the end.if, how can you stop a ZeroDivisionError?str(5), or use a comma in print. (b) NameError. Use name. (c) SyntaxError. Add a colon.if divisor == 0: first, and print a message. Only divide in the else part.Log in to join the discussion.
Loading…