36: Designing and Debugging
Now that you know
if-statements, I'm going to give you some rules for
while-loops that will keep you out of trouble. I'm also going to give you some tips on debugging so that you can figure out problems with your program. Finally, you will design a little game similar to the last exercise but with a slight twist.
From Idea to Working Code
There is a simple process anyone can follow to turn your idea into code. This isn't the only process, but it is one that works well for many people. Use this until you develop your own personal process.
- Get your idea out of your head in any form you understand. Are you a writer? Then write an essay about your idea. Are you an artist or designer? Then draw the user interface. Do you like charts and graphs? Check out the Sequence Diagram, which is one of the most useful diagrams in programming.
- Create a file for your code. Yes, believe it or not this is an important step that most people stumble over. If you can't come up with a name, just pick a random one for now.
- Write a description of your idea as comments, in plain English language (or whatever language is easiest for you).
- Start at the top, and convert the first comment into "pseudo-code", which is kind of Python but you don't care about syntax.
- Convert that "pseudo-code" into real Python code, and keep running your file until this code does what your comment says.
- Repeat this until you've converted all of the comments into Python.
- Step back, and review your code, then delete it. You don't have to do this all the time, but if you get in the habit of throwing away your first version you'll receive two benefits: a. Your second version is almost always better than the first. b. You confirm to yourself that it wasn't just dumb luck. You actually can code. This helps with impostor syndrome and confidence.
Let's do an example with a simple problem of "create a simple fahrenheit to celsius converter". Step one, I would write out what I know about the conversion:
C equals (F - 32 ) / 1.8. I should ask the user for the F and then print out the C.
Very basic math formula which is an easy way to understand the problem. Step 2, I write comments describing what my code should do:
Register for Learn Python the Hard Way, 5th Edition (2023-2024)
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.