๐Ÿ Python for beginners ยท session 4 of 58 ยท 60 minutes ยท ๐ŸŽ free with an account

Talking to the user: input() and types

Programs become fun when they talk to you. Ask a question with input(), turn the answer into a number, and build programs that work for any answer, practising each step as you go.

๐ŸŽ Unlock free: make an account
Illustration of a cute pastel robot dancing
Illustration: andremsdesign / Pixabay

A complete lesson ยท 60 minutes

Teach it, see it, practise it, all on this page.

Open this page with your learner and work down it, or press Teach this lesson to go through it one step at a time on a big screen. Every section has something to do.

  1. Warm-up4 min
  2. Asking a question7 min
  3. Text or number?9 min
  4. Convert straight away8 min
  5. Calculators7 min
  6. Age in 20408 min
  7. Pizza planner8 min
  8. Story machine5 min
  9. Quiz3 min
  10. Set the project1 min

By the end you can

  • Ask a question with input() and store the answer
  • Explain why input() always gives text
  • Convert with int(), float() and str()
  • Build calculators and planners that work for any input

Before you start

  • Session 2: variables and f-strings

Have ready

  • This page (type answers in the Input box under each editor, one per line)

Beginner ยท ages 10 and up

Warm-up ยท think first

A form asks "How old are you?" and you type 11. Then it asks "How old will you be in 5 years?" Can the computer add 5 to what you typed straight away? What might go wrong?

Show the answer

What you type arrives as text, like a word. Adding 5 to the word "11" makes no sense, so the program must first turn the text into a number.

Teacher tip: Write "11" and 11 on paper: which is a word made of characters, and which is a number you can add to?

More warm-ups for every class โ†’

Three levels, like a real engineer

Use it. Tweak it. Build it.

First use what the lesson shows exactly as it is, then change one key thing and see what happens, then make your own.

  1. ๐Ÿ”’

    Level 1 ยท Use it

    Use It Greeter

    Run this exact program: name = input('What is your name? ') then print('Hello, ' + name). Type your name and notice what the computer says back.

  2. ๐Ÿ”ง

    Level 2 ยท Tweak it

    Tweak It Colours

    Change the question text in input('What is your name? ') to 'What is your favourite colour? ' and change 'Hello, ' to 'Nice colour, '. Predict what changes, then run it.

  3. ๐Ÿ› ๏ธ

    Level 3 ยท Build it

    Build It Calculator

    Make your own age-in-10-years calculator. Ask for age with input(), convert it using int(), then print the age plus 10. Test it with your age and a friend's age.

Part 1 of 4 ยท learn, see, practise

Asking a question

input("question") shows the question, waits for an answer and gives it back. Store it in a variable.

On this page, type answers in the Input box, one per line, then press Run. The program takes the next line each time it asks.

Example: run it, then change it

โ–ถ Output

What is your name? Asha
Hello, Asha! Nice to meet you.

๐Ÿ“ฆ Memory boxes

name'Asha'str

Change Asha to your name and run again.

Practise it

Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.

Warm-up

1.1 Ask for a name and print Welcome, <name>!

Task 1.1

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 2 lines the student writes

name = input("Name? ")print(f"Welcome, {name}!")

What it prints when they type Mia

Name? Mia
Welcome, Mia!

What โœ“ Check looks for

  • When they type Mia, the last line it prints is Welcome, Mia!
  • When they type Om, the last line it prints is Welcome, Om!
Core

1.2 Ask for a colour, then an animal. Print: Your <colour> <animal> is ready.

Task 1.2

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 3 lines the student writes

c = input("Colour? ")a = input("Animal? ")print(f"Your {c} {a} is ready.")

What it prints when they type red, dragon

Colour? red
Animal? dragon
Your red dragon is ready.

What โœ“ Check looks for

  • When they type red then dragon, the last line it prints is Your red dragon is ready.
  • When they type blue then whale, the last line it prints is Your blue whale is ready.
Stretch ๐Ÿ”ฅ

1.3 Ask for three words, then print them in reverse order on one line, separated by spaces.

Task 1.3

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ Model answer (for the teacher)

Teacher eyes only: hide the answers before you share your screen.

The answer green = the 4 lines the student writes

a = input("1? ")b = input("2? ")c = input("3? ")print(c, b, a)

What it prints when they type one, two, three

1? one
2? two
3? three
three two one

What โœ“ Check looks for

  • When they type one then two then three, the last line it prints is three two one
  • When they type a then b then c, the last line it prints is c b a

๐ŸŽ Free for a year

Make a free account to open the rest of this lesson

Every new account opens every lesson of every course free for a whole year: no payment, no card, nothing to cancel.

Unlocking the rest of this lesson gives you:

  • Part 2: input() always gives text, with practice at three levels
  • Part 3: Convert straight away, with practice at three levels
  • Part 4: Calculators that work for anyone, with practice at three levels
  • Class activity 1: Age in the year 2040
  • Class activity 2: Pizza party planner
  • Class activity 3: Story machine
  • A 8-question quiz
  • After-class project: Unit converter
  • Homework and a printable practice sheet with answers
  • Teach mode and the minute-by-minute teacher's plan

It takes a minute. Anyone under 13 signs up with a parent. The first 2 lessons of every course, every game and all maths practice stay free.

FAQ

Talking to the user: input() and types: questions

Why does Python input() return a string?

The keyboard sends characters, so input gives text and you convert it with int() or float().

How do I read a number from the user in Python?

Wrap input in int() or float(): age = int(input("Age? ")).

How does the Input box work on this page?

Each line is one answer, used in order whenever the program asks.

Start a conversation

Want a teacher to take this class?

Tell us the learner's age and level. A teacher from our network teaches with these lessons; we confirm timing and price first.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Chat on WhatsApp
Esc

Try