Talking to the Human 💬
So far your programs have monologued. Time to let them listen, and to meet the trap that catches every single beginner exactly once.
input() asks a question
name = input("What is your name? ")
print(f"Nice to meet you, {name}.")
What is your name? Guybrush Threepwood
Nice to meet you, Guybrush Threepwood.
When you press ▶ run here, your browser pops up a box. In a terminal, the program stops
and waits for you to type and press Enter. Either way, input() hands back
whatever the human typed, and you catch it in a variable.
Note the trailing space in "What is your name? ". Without it, the cursor
would sit flush against the question mark and look wrong. Small thing, and people notice.
The trap
Everything input() returns is text. Everything. Always.
age = input("How old are you? ")
print(f"You said: {age}")
print(f"Its type is: {type(age)}")
How old are you? 30
You said: 30
Its type is: <class 'str'>
So this breaks:
age = input("How old are you? ")
print(age + 10)
Traceback (most recent call last):
File "ages.py", line 2, in <module>
print(age + 10)
~~~~^~~~
TypeError: can only concatenate str (not "int") to str
Read the error as English and it is telling the truth precisely: you asked to add a str and an int, and Python refuses to guess which one you meant. Should '30' + 10 be 40, or '3010'? Both are defensible. Python declines to flip a coin on your behalf.
The fix is to convert, explicitly:
age = int(input("How old are you? "))
print(f"In ten years you will be {age + 10}.")
How old are you? 30
In ten years you will be 40.
| You want | Wrap it in | Example |
|---|---|---|
| A whole number | int() | int(input("Age? ")) |
| A decimal | float() | float(input("Price? ")) |
| Text | nothing at all | input("Name? ") |
What if they type nonsense?
int("banana") raises ValueError and your program stops dead.
Level 3 teaches the proper defence (try/except, Lesson 22).
Until then, know that this is the correct instinct to be worried about: any program that
trusts what a human typed is one typo away from a crash.
You are not expected to understand this yet. It is here so that when you reach Lesson 22 it feels familiar rather than new.
raw = input("How many? ")
try:
count = int(raw)
print(f"Right, {count} of them.")
except ValueError:
print(f"'{raw}' is not a number I can use. Try digits only.")
How many? three
'three' is not a number I can use. Try digits only.
Several questions
name = input("Name: ")
ship = input("Ship: ")
crew = int(input("Crew size: "))
print()
print("=" * 34)
print(f" Captain {name}")
print(f" Vessel: {ship}")
print(f" Crew: {crew} ({crew * 2} legs)")
print("=" * 34)
Name: Guybrush
Ship: Sea Monkey
Crew size: 12
==================================
Captain Guybrush
Vessel: Sea Monkey
Crew: 12 (24 legs)
==================================
Two small tricks worth stealing: print() with nothing in it prints an empty
line, and "=" * 34 repeats a character to draw a rule. Multiplying text is
a Python nicety you will use constantly for quick command-line output.
The other half: printing well
print("no", "newline", "between these", end=" -> ")
print("see?")
print("a", "b", "c", sep=" | ")
print("2026", "08", "17", sep="-")
no newline between these -> see?
a | b | c
2026-08-17
end= replaces the newline that print normally adds, and
sep= replaces the space it puts between items. Two keyword arguments,
surprisingly handy, and a first look at a Python feature you meet properly in Lesson 18.
The tavern greeter
Ask for a name and a favourite drink, then print a greeting that uses both, with the name in title case however they typed it.
Reveal solution
name = input("Name: ")
drink = input("Poison of choice: ")
print(f"Welcome to the Scumm Bar, {name.strip().title()}.")
print(f"One {drink.strip().lower()}, coming up.")
Name: gUYBRUSH
Poison of choice: GROG
Welcome to the Scumm Bar, Guybrush.
One grog, coming up.Rectangle calculator
Ask for a width and a height as decimals, then print the area and the perimeter to one decimal place.
Reveal solution
width = float(input("Width: "))
height = float(input("Height: "))
area = width * height
perimeter = 2 * (width + height)
print(f"Area: {area:.1f}")
print(f"Perimeter: {perimeter:.1f}")
Width: 3.5
Height: 4.2
Area: 14.7
Perimeter: 15.4Find the bug
This is meant to add two numbers. Why does entering 2 and 3 produce 23?
a = input("First number: ")
b = input("Second number: ")
print(a + b)Reveal solution
Because a and b are strings, and + on two strings glues them together instead of adding. "2" + "3" is "23", which is correct behaviour for the code that was actually written.
a = int(input("First number: "))
b = int(input("Second number: "))
print(a + b)
First number: 2
Second number: 3
5This bug is a rite of passage. You get it once, you never get it again.