Store the Question
Exit

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.

  1. Create a variable named question and 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.
  2. Replace the hardcoded question text in print() with question — now the display reads from the variable, so changing the question means updating one line at the top, not hunting through print() calls.