Lesson 4 of 12

Getting User Input 👋

🎯 Grades 6–8 ⏱ ~25 minutes 💚 Intermediate

What You'll Learn

  • Use the input() function to get text from users
  • Convert input to a number with int() and float()
  • Build a program that responds to the user

The input() Function

input() pauses your program and waits for the user to type something and press Enter. Whatever they type comes back as a string.

Python
name = input("What is your name? ")
print("Hello, " + name + "!")

Run this and type your name when prompted. You should see a personalized greeting!

Terminal showing Python input/output

Converting to Numbers

input() always returns a string. If you want to do math with the result, you need to convert it:

Python
age_str = input("How old are you? ")
age = int(age_str)          # convert to integer
print("In 10 years you will be", age + 10)
💡
int() vs float()

Use int() for whole numbers, float() for decimals. int('3.14') will cause an error — use float('3.14') instead.

🎯Personal Greeter

Build a program that:

  1. Asks the user's name
  2. Asks the user's age (convert to int)
  3. Prints: "Hi [name]! You will be [age+1] years old next year."
Quick Check

What does input() always return?

AAn integer
BA string
CA float

How do you convert a string to an integer?

Astr()
Bfloat()
Cint()

Which line gets the user's name?

Aname = "Alex"
Bname = input("Your name? ")
Cprint(name)