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:\Python27\python.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:\Python27\python.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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章