Display with Variables
Exit

Display with Variables

Use f-strings to embed variable values in the option display

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

You now have variables for all four options, but the print() statements still use hardcoded text. The variables aren't being used yet. If you renamed option_1 from "int" to something else, the display wouldn't change.

You need a way to insert a variable's value into a string. That's what f-strings do.

How f-strings work

Prefix a string with f and wrap a variable name in {}. Python replaces {name} with the variable's value when the line runs:

color = "blue"
print(f"The sky is {color}.")

Output: The sky is blue.

The f before the opening quote activates the substitution. Without it, {color} is treated as literal text — Python prints the curly braces and all.

Combining a number and a variable

You can mix fixed text and variable references in the same f-string:

rank = 1
label = "gold"
print(f"  {rank}) {label}")

Output: 1) gold

That's exactly the pattern you'll use for the quiz options.

Instructions

Wire the option variables into the display using f-strings.

  1. Replace each option print with an f-string version — once updated, changing option_1 = "int" to any other string will automatically update what the player sees:
    • Replace print(" 1) int") with print(f" 1) {option_1}")
    • Replace print(" 2) str") with print(f" 2) {option_2}")
    • Replace print(" 3) float") with print(f" 3) {option_3}")
    • Replace print(" 4) bool") with print(f" 4) {option_4}")