How to use comments and read errors
Write useful comments
A # starts a comment through the end of the line. Explain intent or a surprising choice instead of restating obvious code.
# Convert minutes to seconds for the timer API.
minutes = 5
seconds = minutes * 60
print(seconds)
Use docstrings
A string at the start of a module, class, or function is a docstring. Tools such as help() can display it.
def square(number):
"""Return number multiplied by itself."""
return number * number
print(square(4))
Read a traceback
Start at the final line for the exception type and message, then move upward to the last line from your own file. That line is where Python noticed the problem.
total = 10
print(totl)
# NameError: name 'totl' is not defined
Recognize common errors
SyntaxError— Python could not parse the code; inspect punctuation near the marker.IndentationError— a block has inconsistent or missing indentation.NameError— a name is misspelled or used before assignment.TypeError— an operation received an unsuitable type.