Skip to content

Instantly share code, notes, and snippets.

@docwhat
Created June 15, 2025 23:48
Show Gist options
  • Save docwhat/0eb66c16af9a35732c40fc6be45e8405 to your computer and use it in GitHub Desktop.
Save docwhat/0eb66c16af9a35732c40fc6be45e8405 to your computer and use it in GitHub Desktop.
An example of a simple menu loop in Python.
#!/usr/bin/env python3
COLORS = {
"r": "red",
"o": "orange",
"y": "yellow",
"g": "green",
"b": "blue",
"i": "indigo",
"v": "violet",
"u": "ultraviolet",
}
def show_choices() -> None:
for key in COLORS.keys():
print(f" {key}. {COLORS[key]}")
def choose_color() -> str:
while True:
# Display the available color choices.
show_choices()
# Prompt the user for a color choice.
choice = input("Choose a color: ").lower()
# Validate the user's choice.
if choice in COLORS.keys():
return COLORS[choice]
else:
print()
print(f"Invalid choice '{choice}'. Please try again.")
def main() -> None:
value = choose_color()
print(f"You chose the color {value}.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment