Common Coding Gotchas
Before we turn you lose on some programming exercises, we’d like to point out some of the most common mistakes beginners seem to make when coding Python statements and programs. You’ll learn to avoid these once you’ve gained a bit of Python coding experience (in fact, Mark commonly gets into trouble because he uses Python syntax in C++ code!); but a few words might help you avoid falling into some of these traps initially.
- Don’t forget the colons
Don’t forget to type a
:
at the end of compound statement headers (the first line of anif
,while
,for
, etc.). You probably will at first anyhow (we did too), but you can take some comfort in the fact that it will soon become an unconscious habit.- Start in column 1
We mentioned this in Chapter 1, but as a reminder: be sure to start top-level (unnested) code in column 1. That includes unnested code typed into module files, as well as unnested code typed at the interactive prompt.
- Blank lines matter at the interactive prompt
Blank lines in compound statements are always ignored in module files, but, when typing code, end the statement at the interactive prompt. In other words, blank lines tell the interactive command line that you’ve finished a compound statement; if you want to continue, don’t hit the Return key at the ... prompt until you’re really done.
- Indent consistently
Avoid mixing tabs and spaces in indentation, unless you’re sure what your editor does with tabs. Otherwise, what you see in your editor may not be what ...
Get Learning Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.