-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_fruit.py
59 lines (45 loc) · 1.43 KB
/
price_fruit.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# price dictionary
fruit_prices = {
"apple" : 0.60,
"orange" : 0.25
}
# sales dictionary
## the number represents which fruit is free
## (2 is buy one, second is free and 3 is buy 2, third is free)
sale_prices = {
"apple": 2,
"orange": 3
}
def onsale(fruit, current_totals):
"""Given the current fruit in the list, return if this fruit is free"""
# add fruit to current totals
if fruit in current_totals:
current_totals[fruit] += 1
else:
current_totals[fruit] = 1
if fruit in sale_prices:
# if this fruit is free, don't add the price to the total
if current_totals[fruit] % sale_prices[fruit] == 0:
print (fruit+" $0.00")
return True
def price_fruit(fruit_list):
"""Given a list of fruit, returns the total price"""
current_totals = {}
total = float(0.00)
for fruit in fruit_list:
fruit = fruit.lower()
try:
if not onsale(fruit, current_totals):
price = fruit_prices[fruit]
print (fruit+" $"+str(price))
total += price
except KeyError as e:
print (fruit+" is not offered at store.")
return float("%.02f"%total)
def main():
fruits = input("Enter the fruit you bought in a comma separated list: ")
fruit_list = fruits.split(',')
total = price_fruit(fruit_list)
print ("Total: $%.02f"%total)
if __name__ == '__main__':
main()