assert 代码自检查

def apply_discount(price, discount):
    updated_price = price * (1 - discount)
    assert 0 <= updated_price <= price, 'price should be greater or equal to 0 and less or equal to original price'
    return updated_price
print apply_discount(100,0.2)

C:Python27python.exe "C:/Users/TLCB/PycharmProjects/untitled2/python study/t22.py"
80.0


def apply_discount(price, discount):
    updated_price = price * (1 - discount)
    assert 0 <= updated_price <= price, 'price should be greater or equal to 0 and less or equal to original price'
    return updated_price
print apply_discount(100,2)

C:Python27python.exe "C:/Users/TLCB/PycharmProjects/untitled2/python study/t22.py"
Traceback (most recent call last):
  File "C:/Users/TLCB/PycharmProjects/untitled2/python study/t22.py", line 6, in <module>
    print apply_discount(100,2)
  File "C:/Users/TLCB/PycharmProjects/untitled2/python study/t22.py", line 4, in apply_discount
    assert 0 <= updated_price <= price, 'price should be greater or equal to 0 and less or equal to original price'
AssertionError: price should be greater or equal to 0 and less or equal to original price
原文地址:https://www.cnblogs.com/hzcya1995/p/13348348.html