Loading...
Loading...
Get user input and display results
So far your programs just run — they don't ask the user for anything. With input(), your program can talk back and forth with the person using it. This is what makes programs interactive instead of one-way.
name = input("What's your name? ")
print(f"Hello, {name}!")| Part | What it does |
|---|---|
| `input("prompt")` | Displays the prompt, waits for the user to type and press Enter |
| The return value | Whatever the user typed — **always as a string** |
| `name = ...` | Stores the user's response in the variable |
age = input("How old are you? ")
print(f"Next year you'll be {int(age) + 1}")Step by step:
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert right away
city = input("Enter your city: ")
print(f"{name} is {age} years old and lives in {city}.")Without input, every program runs identically every time. Input makes programs dynamic:
| ❌ Wrong | Why | ✅ Right |
|---|---|---|
| `age = input("Age? ")` then `age + 1` | `age` is a string! Can't add number | `int(age) + 1` |
| `input("Enter number:")` without space after `:` | Prompt runs right into user's typing — looks messy | Use a space: `": "` |
| `int(input("Age? "))` with no error handling | Crashes if user types "twenty" | (Advanced) Use try/except |
| Forgetting to store the result | `input("Name: ")` alone runs but the input is lost! | Always assign: `name = input(...)` |
Write a program that:
name = input("Enter your name: ")
num = int(input("Enter your favorite number: "))
print(f"Hello {name}! Your number doubled is {num * 2}!")