Store the Question
Move the question text into a variable so it can be reused
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
The question text is hardcoded inside a print() call right now. If you want to change the question, you have to find it buried in the code. If you want to reference it again later — for example, to repeat the question when showing the answer — you'd have to type it out a second time.
Moving it into a variable fixes both problems.
Assigning a string variable
You assign a value to a variable with =. The name goes on the left, the value on the right:
destination = "Paris"Now destination holds the text "Paris". You can use it anywhere in the file.
Printing a variable
To print a variable, pass the variable name to print() — without quotes:
print(destination)Quotes would print the literal text destination. Without quotes, Python looks up the variable and prints its value: Paris.
Once the question lives in a variable, updating the question means changing one line at the top of the file — not hunting through print() calls.
Instructions
Move the question text into a variable.
- Create a variable named
questionand assign it the string"What is the type of the value 3.14?"— this gives the question text a name so you can reference it anywhere without repeating the string. - Replace the hardcoded question text in
print()withquestion— now the display reads from the variable, so changing the question means updating one line at the top, not hunting throughprint()calls.
# Step 1: Create the question variable
print("=== Python Quiz ===")
print()
# Step 2: Print the question variable
print()
print(" 1) int")
print(" 2) str")
print(" 3) float")
print(" 4) bool")
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding