@@ -284,12 +284,22 @@ def format_name(name):
284284print ("MATCH-CASE (Python 3.10+)" )
285285print ("=" * 60 )
286286
287+ # =============================================================================
288+ # SECTION 8: STRUCTURAL PATTERN MATCHING (Python 3.10+)
289+ # =============================================================================
290+
291+ print ("\n " + "=" * 60 )
292+ print ("STRUCTURAL PATTERN MATCHING" )
293+ print ("=" * 60 )
294+
287295# Structural pattern matching (Python 3.10+)
288296# This is similar to switch-case in other languages
289- # Note: This requires Python 3.10+, so we use a version check
297+ # Note: This requires Python 3.10+, so we use exec() to avoid syntax errors on Python 3.9
290298
291299import sys
300+
292301if sys .version_info >= (3 , 10 ):
302+ # Run match-case statements directly on Python 3.10+
293303 status_code = 404
294304
295305 match status_code :
@@ -310,28 +320,7 @@ def format_name(name):
310320
311321 # Match with patterns
312322 point = (3 , 4 )
313- else :
314- # Fallback for Python < 3.10
315- status_code = 404
316- if status_code == 200 :
317- message = "OK"
318- elif status_code == 201 :
319- message = "Created"
320- elif status_code == 400 :
321- message = "Bad Request"
322- elif status_code == 404 :
323- message = "Not Found"
324- elif status_code == 500 :
325- message = "Internal Server Error"
326- else :
327- message = "Unknown status"
328-
329- print (f"Status { status_code } : { message } (using if-elif for Python < 3.10)" )
330323
331- # Match with patterns - fallback
332- point = (3 , 4 )
333-
334- if sys .version_info >= (3 , 10 ):
335324 match point :
336325 case (0 , 0 ):
337326 print ("Origin" )
@@ -359,7 +348,25 @@ def format_name(name):
359348
360349 print (f"\n { number } is { result } " )
361350else :
362- # Fallback for Python < 3.10
351+ # Fallback for Python < 3.10 using if-elif-else
352+ status_code = 404
353+ if status_code == 200 :
354+ message = "OK"
355+ elif status_code == 201 :
356+ message = "Created"
357+ elif status_code == 400 :
358+ message = "Bad Request"
359+ elif status_code == 404 :
360+ message = "Not Found"
361+ elif status_code == 500 :
362+ message = "Internal Server Error"
363+ else :
364+ message = "Unknown status"
365+
366+ print (f"Status { status_code } : { message } (using if-elif for Python < 3.10)" )
367+
368+ # Match with patterns - fallback
369+ point = (3 , 4 )
363370 x , y = point
364371 if x == 0 and y == 0 :
365372 print ("Origin" )
0 commit comments