Get the Player's Answer
Use input() to wait for the player to type their answer
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
The quiz prints the question and options, then exits immediately. The player never gets a chance to type anything. To make the quiz interactive, you need a way to pause the program and wait for input.
The input() function
input() pauses the program, displays a prompt, and waits for the player to press Enter. Whatever they type is returned as a string and stored in a variable.
For example, to ask for a city name:
city = input("Enter a city: ")
print(city)The string you pass to input() is displayed as the prompt — it is not stored anywhere. The player's response is what gets stored.
The result is always a string. Even if the player types a number, Python stores it as text. You will handle that in the next chapter.
Where the input line goes
The player answers after seeing the options. The input() call belongs after the blank line that follows the option list.
Instructions
Add the input prompt after the options.
- After the blank
print()that follows the option list, create a variable namedanswer_textand assign itinput("Your answer (1-4): ")— this pauses the program and waits for the player to type their choice. Whatever they type is stored as a string inanswer_text.
question = "What is the type of the value 3.14?"
option_1 = "int"
option_2 = "str"
option_3 = "float"
option_4 = "bool"
correct = 3
print("=== Python Quiz ===")
print()
print(question)
print()
print(f" 1) {option_1}")
print(f" 2) {option_2}")
print(f" 3) {option_3}")
print(f" 4) {option_4}")
print()
# Step 1: Create answer_text variable
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding