-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathconditionals.py
More file actions
41 lines (35 loc) · 1.03 KB
/
conditionals.py
File metadata and controls
41 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Avoid this:
# def shipping_cost(country, items):
# if country is not None:
# if country == "US":
# if len(items) > 0: # Non-empty cart?
# if len(items) > 10: # Free shipping?
# return 0
# else:
# return 5
# else:
# return 0
# elif country == "CA":
# if len(items) > 0: # Non-empty cart?
# return 10
# else:
# return 0
# else:
# # Other countries
# if len(items) > 0: # Non-empty cart?
# return 20
# else:
# return 0
# else:
# raise ValueError("invalid country")
# Favor this:
def shipping_cost(country, items):
if country is None:
raise ValueError("invalid country")
if not items: # Empty cart?
return 0
if country == "US":
return 0 if len(items) > 10 else 5
if country == "CA":
return 10
return 20 # Other countries