Master the basics of getting information from a user and displaying results back to them.
What if your computer wasn't just a screen you looked at, but a partner you could actually talk to? Imagine writing a single line of code that allows your computer to learn your name and greet you like a friend.
By default, Python programs run from top to bottom without stopping. To make a program interactive, we use the input() function. Think of this function as a 'pause button.' When Python hits an `input()` line, it stops and waits for the user to type something and press the Enter key. The text the user types is then handed back to the program as a string. Even if the user types a number like , Python treats it as text characters '4' and '2' unless we tell it otherwise. We usually store this information in a variable so we can use it later, like this: .
1. Create a variable named `fav_food`. 2. Use the `input()` function to ask the user a question. 3. Code: `fav_food = input("What is your favorite snack? ")`. 4. When run, the computer displays the text and waits for you to type your answer.
Quick Check
What specific action must the user take to tell Python they are finished typing their input?
Answer
The user must press the Enter (or Return) key.
Once you have captured data in a variable, you need a way to show it back to the user. In the past, joining text and variables was messy. Now, we use f-strings (formatted strings). To create one, put the letter right before the opening quotation mark. This tells Python to look inside the string for curly braces . Anything inside the braces is treated as code or a variable name. For example, if , then becomes "Your score is 10". It is a clean, fast way to build sentences that change based on user data.
1. Collect the user's name: `name = input("Name: ")`. 2. Collect their city: `city = input("City: ")`. 3. Use an f-string to combine them: `print(f"Hello {name} from {city}!")`. 4. If the user enters 'Alex' and 'London', the output is: 'Hello Alex from London!'
Quick Check
In the f-string f"Total: {price}", what does the 'f' at the beginning signify?
Answer
It tells Python that the string is 'formatted' and contains variables inside curly braces.
Interactive programs are most powerful when they use multiple pieces of data to create a unique experience. You can use the input() function multiple times in a row to gather a 'profile' of the user. Remember that the order matters: you must define a variable using `input()` before you try to use it in a `print()` statement. If you try to print a variable that hasn't been assigned yet, Python will throw a NameError. Good interactive design also includes a space at the end of your input prompt string so the user's typing doesn't touch your question text.
1. Ask for a hero name: `hero = input("Name your warrior: ")`. 2. Ask for a weapon: `weapon = input("Choose a weapon: ")`. 3. Ask for a power level: `power = 9001`. 4. Combine all three: `print(f"{hero} grabs the {weapon} with a power of {power}!")`. 5. This creates a dynamic story starter based entirely on user choices.
Which line of code correctly asks for a user's age and stores it?
If user_color = 'Blue', what is the correct syntax for an f-string?
True or False: If a user types the number 10 into an input() prompt, Python stores it as an integer (number) automatically.
Review Tomorrow
In 24 hours, try to explain to a friend why we use curly braces {} in Python and what the 'f' stands for in an f-string.
Practice Activity
Build a 'Mad Libs' game: ask the user for an adjective, a noun, and a verb, then use an f-string to put them into a funny sentence.