Skip to content
Crayon CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

Lesson 4 of 26 · about 12 min

Talking to the user

input() asks the user a question and always gives back text. Convert it with int() or float(), and build answers with f-strings.

🧭 What you need first: Variables: names for values

The story

So far your programs have talked at people. Now they’ll listen.

Picture a café cashier taking your order on a notepad. Whatever you say (“two lattes”, “3”, “a dozen”), they write down as words. If the manager later wants to add up how many coffees were sold, someone has to turn the scribbled “3” into the number 3 first.

Python’s input() is that notepad. Whatever the user types (even digits) comes back as text. If you want to do math with it, you convert it to a number first.

Another way: a web form

When you fill in your age on a website, the browser sends “36” as text. The website’s code has to turn it into a number before it can check “is this person over 18?” Every program that reads human input does this.

Another way: a recipe card

“Bake for 20 minutes” is written in ink, not a timer. To actually set the oven timer, you read the “20” and turn it into a timer setting. Converting text to a number is the same move.

Play with it

A program that listens

The box under the editor pretends to be the user. Each line is one answer. Run it, then change the answers and run it again.

Now try removing the int(...) around the second input. Python complains, because you can’t add the number 1 to the text “36”.

Name it

Grown-up words

  • input(prompt) shows the prompt, waits for typing, and returns a string.
  • Type conversion (also called casting) turns a value into another type:
    • int("42") → 42 (fails on "4.5" or "hello")
    • float("4.5") → 4.5
    • str(42) → "42"
    • round(3.14159, 2) → 3.14
  • An f-string (formatted string) has an f before the quote. Anything inside { } is worked out and inserted:
    • f"{name} is {age}" → "Ada is 36"
    • f"{price:.2f}" → always 2 decimal places, like "7.50", which is perfect for money.
  • print() can take several things separated by commas; it puts a space between them. print("a", "b", sep="-") uses a dash instead.

Input → process → output is the shape of almost every program: get data, work on it, show a result.

Build it

Build a tip calculator

Ask for the bill amount and the tip percentage. Store the bill as a float in bill, the tip amount in tip, and bill + tip in total. Print both with 2 decimal places.

Challenge

Spot the type mix-up

The user types 5. What does this print? n = input("Number? ") then print(n * 3)

Remember it

What type does input() always return?

Got it?

Finish the lesson to color it in on your map and collect its concept cards.

Did this lesson click?