Undoubtedly the majority of the time spent by students on programming tasks is not so much spent on the programming as on finding and fixing the little mistakes that seem to proliferate in their code. There are a few tricks veteran programmers use to minimize the amount of time they spend debugging, which will be described in this section.
(Bear in mind that in programming as in many things, prevention is the best medicine; planning your program ahead of time and structuring it in a straightforward, understandable way will drastically cut down on the number of bugs you will have to squish.)
Python allows you to test certain blocks of code individually and return a custom error message if the block runs into some error. An important part of the try/except block is that your code can continue running after it.
while True: try: number = int(input("Enter a number of eggs: ")) break except ValueError: print("Please enter a number this time!")
The above code will loop until a valid number is provided.
This is an actually useful way of writing user-proof code. But you can also use the try/except blocks for debugging:
try: #some code that might not be working right except: print("Something's wrong in that block!")
You can also add a "finally" clause, to be executed irrespective of whether or not the exception occurred (even if there's a break or continue statement in the try or except clauses):
try: #some code that might not be working right except: print("Something's wrong in that block!") finally: print("Thanks for running my code!")
8.2. Best practices
Here's some general advice about debugging. If you're stuck and your program's not doing the right thing, or not running at all, try the ideas listed here.
- Make sure you have a plan before you start to code. A flowchart may be overkill, but know what you're doing.
- Put as many multi-step operations as possible into functions, and test the functions individually. Smaller bits of code, involving very few variables, are a lot easier to make sense of.
- Use the "try/except" method described above to figure out where your bugs are coming from.
- Try to predict the silly things your users will do, and make your program user-proof
- Make liberal use of "useless" print statements during the development phase to figure out what values your variables are holding. When you have a complicated loop that modifies a lot of data, it helps a lot to try to figure out by hand what the values of your variables should be and compare that with what they actually are.
- Make sure your IF statements all use double equals signs (==) rather than single equals signs! This is a VERY common problem.
- Try to avoid global variables in your functions, since they result in confusing code with variables being modified by code very far away
- Make sure you always know what data type your variables are and your functions expect; remember, in most versions of Python, 2/1 = 0!
- Ensure your loops terminate (You'll know when you get this one wrong--your program will just hang there!)
- Don't get (too!) discouraged!