Introduced in Python 3.10, the match-case statement provides a cleaner, more readable alternative to writing long chains of elif statements when checking a single variable against multiple exact values.
The case _ acts as the default wildcard catch-all (similar to else).
case _ is not mandatory but good to keep
Build an interactive calculator menu. The user enters two numbers and a mathematical operator symbol (+, -, *, /, **). Use a match-case statement on the operator string to perform the correct calculation and print the result. Include a check for division by zero.
Expected Output:
Enter first number: 10
Enter second number: 5
Enter operator (+, -, *, /, **): *
Result: 10 * 5 = 50.0
Write a Python program that simulates an automated vending machine.
First, print a neat menu displaying the available snacks, their codes, and prices.
Then, prompt the user to enter a snack code ("A1", "B2", "C3", or "D4").
Use a match-case statement to check the code, display the dispensed item, and handle invalid entries using the default case (_).
Expected Output:
========================================
SMART VENDING MACHINE - ITEM LIST
========================================
[A1] Chocolate Bar - $1.25
[B2] Potato Chips - $1.50
[C3] Sparkling Water - $2.00
[D4] Gummy Bears - $1.75
----------------------------------------
Enter snack code (A1, B2, C3, D4): B2
Dispensing: Potato Chips - Price: $1.50
Enjoy your snack!
Write a Python program that simulates a smart recycling station sorter.
First, print a numbered menu titled RECYCLING BIN MATERIAL SORTER MENU displaying the available waste materials and their corresponding numbers.
Then, prompt the user to enter a choice number (1 through 4).
Use a match-case statement to match the number to the correct recycling bin color and category, handling invalid choices with the default case (_).
Expected Output :
==================================================
RECYCLING BIN MATERIAL SORTER MENU
==================================================
[1] Plastic
[2] Glass
[3] Paper
[4] Organic
--------------------------------------------------
Enter material number (1-4): 2
🟢 Place in the Green Bin: Recyclable Glass Container.