Python Basics · Lesson 5 of 12

Asking for Input

A program becomes far more useful when it can ask a question. input() shows a prompt, waits for an answer, and gives the answer back as a string:

name = input("What is your name? ")
print(f"Nice to meet you, {name}!")

Where the answers come from

On your own computer, the program would pause and wait for you to type. In this editor, the answers come from the Input for the Program box under the code. Put one answer on each line: the first input() reads the first line, the second reads the second line, and so on.

Input is always text

Whatever the person types, input() returns a string. That causes a famous beginner surprise:

age = input("How old are you? ")   # the person types 20
print(age + 1)                     # error!

Python refuses to add a number to a string. Convert the text first:

  • int("20") turns a string into a whole number.
  • float("2.5") turns a string into a decimal number.
  • str(20) goes the other way, from a number to a string.
age = int(input("How old are you? "))
print(age + 1)   # 21

Wrapping input() in int() like this is something you will write constantly.

If the text is not a number, int() stops the program with a ValueError. Handling that properly is a topic for later.

Your Task

  1. Ask for the user's name with input() and store it in name.
  2. Ask for the year they were born, convert it to a whole number and store it in year.
  3. Create age as 2025 minus year.
  4. Print exactly: Mia will turn 15 in 2025. (using the name and the age from your variables).
  • The program calls input() twice
  • name holds the first answer
  • year is a whole number, not a string
  • age is 2025 minus the year
  • The program prints Mia will turn 15 in 2025.