BigSteve/Python/026_matchnested.py

28 lines
883 B
Python

# Case, switch, match example
# Note: Python's match implementation was introduced in Python version 3.10
num = None
# Note that this has a nested structure. There is an outer while loop
# which contains an inner try/except block. Structures can be nested
# at nearly infinite complexity, but if the structure gets more than
# a short few nestings it gets confusing to read and functions
# (dicussed later) should probably be used at that point.
while num is None:
try:
num = int(input("Enter a number between 1 and 3: "))
except:
print(" Oops, that's not a number...")
match num:
# match 1
case 1:
print("You entered One")
# match 2
case 2:
print("You entered Two")
# match 3
case 3:
print("You entered Three")
# match default (i.e. no match)
case _:
print("You did not follow directions!")