Skip to main content

Ex 12: Menu Price Lookup

Problem

We’re creating a menu price lookup system for a food ordering app. We need to use the match-case statement to handle multiple menu items.

Rules

  • Define get_item_price(item: str) -> str.
  • Normalize the input by using .lower().strip().
  • Return "Price: 15 bucks" for a "burger".
  • Use case _ to return "Item not available" for anything else.

Boilerplate

Copy below code and paste to our IDE for head start.

# Write our code inside this function
def get_item_price(item: str) -> str:
pass # remove this

# Call with test data
print(get_item_price("burger"))
print(get_item_price("taco"))

Expected Output

Price: 15 bucks
Item not available

Solution

Only view the solution after trying our best.

Show solution
def get_item_price(item: str) -> str:
match item.lower().strip():
case "pizza":
return "Price: 30 bucks"
case "burger":
return "Price: 15 bucks"
case "pasta":
return "Price: 20 bucks"
case "salad":
return "Price: 10 bucks"
case _:
return "Item not available"

# Test calls
print(get_item_price("burger"))
print(get_item_price("taco"))

Details

Normalizing input directly in the match expression (match item.lower().strip():) is a common and clean Python pattern. It ensures that the subsequent case branches only have to deal with sanitized, lowercase strings.