Zum Inhalt springen
KodeTrail

Camp 1 · Schritt 4 von 23

Comments and error messages

Leave notes in your code with comments — and learn to read error messages like a friendly guide, not a scolding.

12 Min.+50 XPAuf Englisch angezeigt — Übersetzung ist unterwegs

Two skills separate people who stick with programming from people who quit in week one: writing notes to your future self, and staying calm when the computer complains. Let's build both.

Comments: notes Python ignores

Anything after a # on a line is a comment — Python skips it entirely. Comments exist for humans:

Python

Three everyday uses:

  1. Explaining why# freeze one day per week so streaks survive rest days
  2. Temporarily disabling code — put # in front of a line to "switch it off"
  3. Leaving TODO markers# TODO: handle negative numbers

Your first error message

Run this — it's broken on purpose:

Python

You'll get a SyntaxError — Python's way of saying "I couldn't even understand this as Python." Here it means a ) is missing. Add it and run again.

Reading a traceback

Now a different kind of error:

Python

This is a NameError: the code is valid Python, but mesage doesn't exist (we never created it — and we probably meant something spelled differently). Error messages follow a pattern:

PartWhat it tells you
Error type (NameError)The category of problem
The messageWhat exactly went wrong
The line numberWhere to look

That's not the computer scolding you. It's the computer filing a very precise bug report, for free, in milliseconds. Experienced programmers see error messages all day long — the skill isn't avoiding them, it's reading them.

Checkpoint

Python shows: NameError: name 'contn' is not defined. What's the most likely cause?

Checkpoint

What does the line: # print('hi') do when the program runs?

Camp 1 complete! 🏕️

You know what programming is, you've written real programs, and you can read error messages. That's the whole first camp. Next up: variables — teaching Python to remember things.