|
|
def apply_discount(price, discount):
# 1. 檢查 price 是否為數字
if not isinstance(price, (int, float)) or isinstance(price, bool):
return "The price should be a number"
# 2. 檢查 discount 是否為數字
if not isinstance(discount, (int, float)) or isinstance(discount, bool):
return "The discount should be a number"
# 3. 檢查 price 是否大於 0
if price <= 0:
return "The price should be greater than 0"
# 4. 檢查 discount 是否介於 0 到 100
if discount < 0 or discount > 100:
return "The discount should be between 0 and 100"
# 5. 計算折扣後的價格
final_price = price - (price * discount / 100)
# 6. 回傳最終價格
return final_price
# 測試程式
print(apply_discount(50, 20))
print(apply_discount(100, 10))
print(apply_discount(200, 50))
print(apply_discount(50, 100))
|
|