We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 22f8aac commit c035daaCopy full SHA for c035daa
Scripts/chapter3/looping/Util/24_coupons.py
@@ -5,3 +5,17 @@
5
dict(id=4, total=110, coupon_code='F15'), # F15: fixed, $15
6
]
7
8
+forcustomerincustomers:
9
+code=customer['coupon_code']
10
+ifcode=='F20':
11
+customer['discount'] =20.0
12
+elifcode=='F15':
13
+customer['discount'] =15.0
14
+elifcode=='P30':
15
+customer['discount'] =customer['total'] *0.3
16
+elifcode=='P50':
17
+customer['discount'] =customer['total'] *0.5
18
+else:
19
+customer['discount'] =0.0
20
21
+print(customer['id'], customer['total'], customer['discount'])
Scripts/chapter3/looping/Util/25_coupons_dict.py
@@ -0,0 +1,28 @@
1
+customers= [
2
+dict(id=1, total=200, coupon_code='F20'), # F20: fixed, $20
3
+dict(id=2, total=150, coupon_code='P30'), # P30: percent, 30%
4
+dict(id=3, total=100, coupon_code='P50'), # P50: percent, 50%
+dict(id=4, total=110, coupon_code='F15'), # F15: fixed, $15
+]
+
+discounts={
+'F20': (0.0, 20.0), # each valuen is (percent, fixed)
+'P30': (0.3, 0.0),
+'P50': (0.5, 0.0),
+'F15': (0.0, 15.0)
+}
+# for customer in customers:
+# code = customer['coupon_code']
+# percent, fixed = discounts.get(code,(0.0, 0.0))
+# customer['discount'] = percent * customer['total'] + fixed
+# print(customer['id'], customer['total'], customer['discount'])
22
23
+foriincustomers:
24
+code=i['coupon_code']
25
+percent, fixed=discounts.get(code,(0.0, 0.0))
26
+i['discount'] =percent*i['total'] +fixed
27
28
+print(i['id'], i['total'], i['discount'])
Scripts/chapter3/looping/Util/26_infinite.py
@@ -0,0 +1,6 @@
+fromitertoolsimportcount
+fornincount(5, 3):
+ifn>20:
+break
+print(n, end=', ') # instead of newline, comma and space
Scripts/chapter3/looping/Util/27_compress.py
@@ -0,0 +1,13 @@
+fromitertoolsimportcompress
+data=range(10)
+even_selector= [1, 0] *10
+odd_selector= [0, 1] *10
+even_numbers=list(compress(data, even_selector))
+odd_numbers=list(compress(data, odd_selector))
+print(even_selector)
+print(odd_selector)
+print(list(data))
+print(even_numbers)
+print(odd_numbers)
Scripts/chapter3/looping/Util/28_permutations.py
@@ -0,0 +1,3 @@
+fromitertoolsimportpermutations
+print(list(permutations('ABC')))
0 commit comments