When you need to get input from the user, you can use the built in input() function. To prompt the user and store their response as a string tied to a variable use the following syntax.
variable_name = input("A question goes here?")
This will display the question in the terminal, pause the program ,and wait for the user to enter some text. Once they press Enter, their response is saved in the variable as a string.
A trick to add an input prompt that is longer than one line is to create the prompt variable, keep adding new
lines to it and then use it as an argument in the
input()
function.
prompt = "message"
prompt += "\nMore message"
input(prompt)
Once you've captured the input, you'll often want to respond to what the user has given you. Traditionally, you'd use if/elif/else statements to make decisions, but since Python 3.10, the match statement offers a cleaner way to handle multiple conditions.
choice = input("Do you want to start, stop, or quit? ").lower()
match choice:
case "start" | "go":
print("Starting the program...")
case "stop":
print("Stopping the program.")
case "quit":
print("Exiting. Goodbye!")
case _:
print("Invalid option. Please try again.")
- input() collects the user's choice.
- .lower() ensures the input is lowercase for consistent matching.
- match checks the value of choice.
- If it's "start" or "go" → run the start code.
- If it's "stop" → run the stop code.
- If it's "quit" → exit.
- The case _: is the default (like else) and runs if nothing else matched.