【发布时间】:2019-03-22 07:28:59
【问题描述】:
我创建了这个遍历字典的函数。我有一个价格字典,存储了一个项目的每个价格。然后是客户订单字典,每个客户的每个订单都存储在其中。
对于客户订单中的每件商品,我将商品乘以价格, 比如……
order 1: 10 books * $10.0
在此功能结束时,如果总订单超过 100 美元,我将给予 10% 的折扣。 50 美元以上 5%,低于 50 美元无折扣。
现在,由于某些原因,我无法更改下面的断言语法。必须坚持使用其中的格式和代码,问题是我遇到了断言错误。应该是,最终输出是“DONE”
如何避免在这个阶段捕获断言?
特别是我在 order1 中遇到断言错误
这就是我所做的......
def calculate_price(price, order):
final_list = []
# Iterating through each dictionary key.
for key, order_value in order.items():
# Splitting on whitespace, taking the first result.
first_word = key.split()[0]
# Initiating condition to compare key similarities.
if first_word in price:
# Matching dict keys successful.
price_value = price[first_word]
# Multiplying key-pair values of two matched keys.
individual_price = (order_value*price_value)
final_list.append(individual_price)
new = sum(final_list)
if new >= 101:
order3 = new - (new*0.10)
elif new >= 51:
order1 = new - (new*0.05)
order1 = int(order1)
else:
order2 = new
price = {'book': 10.0, 'magazine': 5.5, 'newspaper': 2.0}
order1 = {'book': 10}
order2 = {'book': 1, 'magazine': 3}
order3 = {'magazine': 5, 'book': 10}
assert(95 == calculate_price(price, order1))
assert(26.5 == calculate_price(price, order2))
assert(114.75 == calculate_price(price, order3))
print("Done")
非常感谢您的建议和帮助。谢谢
【问题讨论】:
-
哪个断言失败了?
-
这是断言顺序1
-
所以你想关闭断言?这可以通过
-o标志来完成,即python -o myfile.py,参见here -
嗨 nearoo,我想结束我的代码,通过所有断言为真
标签: python python-3.x assert assertion