-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.10.py
More file actions
91 lines (80 loc) · 1.96 KB
/
6.10.py
File metadata and controls
91 lines (80 loc) · 1.96 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def list_fruit(prices, stock):
"""
包含水果名称、单价、库存
:param prices:水果价格 dict
:param stock: 水果库存 dict
:return:
"""
# 两个字典的key是相同的,所以只需要遍历一个字典即可
for key in prices:
# for key in prices and stock:
print(key)
print("售价 {}".format(prices[key]))
print("现有数量 {}".format(stock[key]))
def total_money(prices, stock):
"""
库存水果的总价
:param prices: 水果价格
:param stock: 水果库存
:return: 库存水果总价
"""
total = 0
for key in prices and stock:
total += prices[key] * stock[key]
return total
def compute_bill(prices, stock, food):
"""
订单总价
:param prices: 水果价格 dict
:param stock: 水果库存 dict
:param food: 订单 list
:return:订单总价
"""
total = 0
for f in food:
if f in prices and stock:
if stock[f] > 0:
total += prices[f]
stock[f] -= 1
return total
def compute_bill_1(prices, stock, food):
"""
订单总价
:param prices: 水果价格 dict
:param stock: 水果库存 dict
:param food: 订单 dict
:return:订单总价
"""
total = 0
for f in food:
if f in prices:
if stock[f] - food[f] > 0:
total += prices[f] * food[f]
stock[f] -= food[f]
else:
total += prices[f] * stock[f]
stock[f] = 0
return total
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3,
}
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15,
}
list_fruit(prices, stock)
print(total_money(prices, stock))
food = ["banana", "orange", "apple"]
food_dict = {
"banana": 8,
"apple": 3,
"orange": 2,
}
# print(compute_bill(prices, stock, food))
print(4 + 1.5)
print(compute_bill_1(prices, stock, food_dict))