42 versus "42"

The single most common beginner bug: treating text that looks like a number as an actual number. Watch what + does to each type.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Numbers add. Strings glue together (concatenate). Add a line that glues your first and last name together with a space between them, and one that adds your age to 10.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

✦ Why this matters

Every form field on the web hands your program a STRING, even when someone types their age. Before you do math with it, you must convert it. Module 17 covers conversion deeply; for now, just recognize the trap.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Remove the conversion line and use the raw text in the math. Run it in BOTH languages, they fail differently. Notice how.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Booleans, and a Glimpse of Collections →
← Back