Coding Pirates

Lesson 2

Constants

Some values should never change while the program runs: the number of seconds in a minute, the maximum number of players, the speed of light.

from typing import Final

MAX_PLAYERS: Final = 4
GAME_NAME: Final = "Treasure Hunt"
  • Constants are written in CAPITAL_LETTERS. That tells other programmers: "do not change this".
  • Python does not lock the box by itself. Adding : Final tells ty to warn you in Zed if you accidentally change it anyway.
  • Why bother? If the number 60 is used in ten places and you write SECONDS_PER_MINUTE instead, everyone can see what the number means.
1 / 3