else adds a block that runs when the if condition is False:
def entry_fee(age):
if age < 18:
return 50
else:
return 120
print(entry_fee(12))
print(entry_fee(30))
Output:
50
120
Exactly one of the two blocks runs. else has no condition of its own — it covers every case the if did not.
When both blocks return, the else can be dropped: code after a return inside the if only runs when the condition was False.
def entry_fee(age):
if age < 18:
return 50
return 120
Both versions behave identically. Use whichever you find clearer.
shipping_cost with one parameter, order_total.order_total is 500 or more, return 0 — shipping is free.59.Run your code to see the output, then press Submit.
import unittest
class TestShippingCost(unittest.TestCase):
def test_600_ships_free(self):
self.assertEqual(shipping_cost(600), 0)
def test_200_costs_59(self):
self.assertEqual(shipping_cost(200), 59)
def test_exactly_500_ships_free(self):
self.assertEqual(shipping_cost(500), 0)
def test_499_costs_59(self):
self.assertEqual(shipping_cost(499), 59)
def shipping_cost(order_total):
if order_total >= 500:
return 0
else:
return 59
print(shipping_cost(600))
print(shipping_cost(200))
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.