Lesson 4 of 12
Getting User Input 👋
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!
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:
- Asks the user's name
- Asks the user's age (convert to int)
- Prints: "Hi [name]! You will be [age+1] years old next year."
Quick Check
What does input() always return?
How do you convert a string to an integer?
Which line gets the user's name?