Welcome to Day 5 of Fluffy Python! So far, our programs have been quite one-sided, just printing things. Today, we're making them interactive by learning how to get information directly from the user using the input() function.
The input() function is a built-in Python function that does two things:
- It displays a message (a "prompt") to the user in the console.
- It then pauses the program's execution and waits for the user to type something and press the Enter key.
- Whatever the user types (before pressing Enter) is captured and returned by the
input()function.
Basic Syntax:
variable_name = input("Your prompt message here: ")This is one of the most common pitfalls for beginners! No matter if the user types "123", "3.14", or "hello", the input() function will always return a string (str) data type.
If you need to perform mathematical operations with the user's input, you must convert (cast) the string to an int or float using int() or float() functions:
# Example: Getting an age
age_str = input("How old are you? ") # age_str will be a string like "25"
age_int = int(age_str) # Convert "25" to integer 25
print(f"Next year, you will be {age_int + 1}!")- Your Own Story: Create a simple interactive story where you ask the user for 3-4 different words (e.g., a place, an animal, an action) and then embed them into a short story that you print.
- Temperature Converter (Partial): Ask the user for a temperature in Celsius. Convert this input to a
float. (You don't need to convert it to Fahrenheit yet, just practice the input and conversion). - Basic Arithmetic Drill: Ask the user for two numbers. Convert them to
intorfloat. Then, print the sum, difference, product, and quotient of those two numbers. - Yes/No Question: Ask the user a simple yes/no question. Store their answer in a variable and print it back to them.
- User Prompts: Always provide clear and concise prompts inside the
input()function so the user knows what information you're asking for. - Error Handling (Future Topic): What if the user types "hello" when you expect a number? Currently,
int("hello")orfloat("hello")would cause aValueError. In professional code, you'd usetry-exceptblocks (a future topic!) to gracefully handle such invalid inputs. - Data Validation: Beyond just type conversion, you might need to validate the value of the input (e.g., is an age positive? Is a quantity greater than zero?). This also ties into error handling.
- Strip Whitespace: Sometimes, users accidentally add spaces before or after their input. You can use
.strip()on the input string to remove leading/trailing whitespace:name = input("Your name: ").strip().
- Open your terminal or command prompt.
- Navigate to the
Day_05_UserInputdirectory.cd path/to/your/fluffy-python/Day_05_UserInput - Run the script using:
python user_input.py # Or if you installed python3: # python3 user_input.py
Tomorrow, we'll wrap up Week 1 with a self-reflection post and a look at our progress. Get ready to celebrate your first week of consistent learning!