Skip to main content

Ex 8: Free Dessert Offer

Problem

We’re designing a billing system for a restaurant. Depending on the total bill amount entered by the customer, they might get a free dessert.

Rules

  • Define get_delivery_offer(bill_amount: float) -> str.
  • If the bill amount is greater than 500, return "We get a free dessert!".
  • Otherwise return "No free dessert this time.".

Boilerplate

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

# Write our code inside this function
def get_delivery_offer(bill_amount: float) -> str:
pass # remove this

# Call with test data
print(get_delivery_offer(600))
print(get_delivery_offer(450))

Expected Output

We get a free dessert!
No free dessert this time.

Solution

Only view the solution after trying our best.

Show solution
def get_delivery_offer(bill_amount: float) -> str:
if bill_amount > 500:
return "We get a free dessert!"
return "No free dessert this time."

# Test calls
print(get_delivery_offer(600))
print(get_delivery_offer(450))

Details

Since return immediately exits the function, we don't need an else block here. If the if condition is true, the first string is returned and the function stops. Otherwise, it simply falls through to the final return.