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
- Ask for the user's name with
input()and store it inname. - Ask for the year they were born, convert it to a whole number and store it in
year. - Create
ageas2025minusyear. - Print exactly:
Mia will turn 15 in 2025.(using the name and the age from your variables).
- The program calls
input()twice nameholds the first answeryearis a whole number, not a stringageis 2025 minus the year- The program prints
Mia will turn 15 in 2025.