Input and Output
When you build a software application, you need a way for the program to talk to the user and for the user to talk back. This two-way communication is handled by Input and Output.
Input: Getting Data From the User
- What is it? This is how your program receives information from the end user. Think of it like your program asking a question.
- The Key Python Tool: We use the built-in function
input(). The program pauses, waits for the user to type something, and then stores that typed data.
Example: Getting the User's Name
# The message inside the input() function is the prompt
user_name = input("Hello! Please enter your name: ")
# Once the user types their name (e.g., 'Job') and presses Enter,
# that name is stored in the 'user_name' variable.
Output: Showing Data to the User
- What is it? This is how your program sends information back to the end user. Think of it like your program giving an answer or displaying a result.
- The Key Python Tool: We use the built-in function
print()to display text, numbers, and variables on the screen (the console).
Example: Displaying a Welcome Message
# 1. Printing simple text
print("Welcome to the program!")
# 2. Printing the variable collected in the input example
# The print function joins the text (string) with the variable value
user_name = input("Please enter your name: ")
print("It's great to meet you,", user_name)
# If 'user_name' was 'Alice', the output is:
# It's great to meet you, Alice
Combining Input and Output (A Simple Program)
| Code | User Action | Program Output |
|---|---|---|
favorite_color = input("What is your favorite color? ") | User types blue and presses Enter. | Prompts: What is your favorite color? |
print("That's a lovely color!") | That's a lovely color! | |
print("You chose:", favorite_color) | You chose: blue |
