🚀 Alguni Start learning

Unit 7: Talking to the Player

Programs that ask questions.

Unit 7 of 31 in Python for kids. Its 5 lessons are Ask a Question, Changing Types, When Things Go Wrong, Keep Asking and Quiz Master — below is everything each one explains, and a question or two from it to try.

Every sample on this page was run through real Python before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

🙋 Ask a Question

Waiting for an answer

input() stops and waits for the player to type something and press enter. Whatever they typed comes back to you. Here the player typed Ada.

Python

name = input()
print("Hi " + name)

It prints

Hi Ada

The classic trap

The player types 5 and you get the text "5", not the number 5. Text times 2 repeats it, which is almost never what you wanted.

Python

n = input()
print(n * 2)

It prints

55

Try it yourself

What does input() give you back?

  • A number
  • Whatever the player typed, always as text
  • True or False
  • Nothing

How do you turn what they typed into a real number?

  • number(n)
  • int(n)
  • num(n)
  • n + 0

Answer them in the app

🔄 Changing Types

Turning one kind into another

int() makes a whole number, float() keeps the decimals, and str() turns anything back into text.

Python

print(int("7") + 1)
print(float("2.5") + 1)
print(str(7) + "!")

It prints

8
3.5
7!

int() throws the decimals away

Turning a decimal into a whole number does not round — it chops. int(3.9) is 3, not 4. Use round() when you want the nearest.

Python

print(int(3.9))
print(round(3.9))

It prints

3
4

Try it yourself

What does this print?

Python

print(int("10") + int("5"))

And this? Look closely — no int() this time.

Python

print("10" + "5")

Answer them in the app

🛟 When Things Go Wrong

Catching a crash

If the player types banana when you asked for a number, int() gives up and your program stops. try / except lets you catch that and carry on instead.

Python

try:
    n = int("banana")
    print(n)
except ValueError:
    print("That is not a number!")

It prints

That is not a number!

Nothing goes wrong, nothing is caught

When the try block works, the except part is skipped entirely — it only runs if something actually breaks.

Python

try:
    n = int("42")
    print(n)
except ValueError:
    print("broken")

It prints

42

Try it yourself

What does this print?

Python

try:
    print(10 / 0)
except ZeroDivisionError:
    print("cannot divide by zero")

What is ValueError?

  • A kind of variable
  • The name of the problem when a value is the wrong shape, like int("cat")
  • A Python command
  • A number

Answer them in the app

🔁 Keep Asking

Asking until they get it right

Put input() inside a while loop and you can keep asking until the answer is sensible. break gets you out once it is.

Python

while True:
    answer = input()
    if answer == "yes":
        print("Great!")
        break
    print("Try again")

It prints

Try again
Great!

Counting the tries

Keep a counter alongside the loop and you can tell the player how many goes they took.

Python

tries = 0
while True:
    tries = tries + 1
    if tries == 3:
        break
print(tries)

It prints

3

Try it yourself

Why does while True: not run forever here?

  • Python stops it automatically
  • The break leaves the loop as soon as the answer is right
  • True eventually becomes False
  • It does run forever

The player types 2, then 5. What does this print?

Python

while True:
    n = int(input())
    if n > 3:
        print("big enough")
        break
    print("too small")

Answer them in the app

🏆 Quiz Master

Try it yourself

The player types 7. What does this print?

Python

n = input()
print(n + n)

Answer it in the app