|
|
https://chatgpt.com/share/6aec1317-6dfc-83e8-975d-aa8ec0ee3c58
def apply_discount(price, discount):
# 檢查 price 是否為數字
if not isinstance(price, (int, float)):
return "The price should be a number"
# 檢查 discount 是否為數字
if not isinstance(discount, (int, float)):
return "The discount should be a number"
# 檢查商品價格是否大於 0
if price <= 0:
return "The price should be greater than 0"
# 檢查折扣是否介於 0 到 100
if discount < 0 or discount > 100:
return "The discount should be between 0 and 100"
# 計算折扣金額
discount_amount = price * (discount / 100)
# 計算折扣後的最終價格
final_price = price - discount_amount
return final_price
# 測試函式
print(apply_discount(100, 20))
print(apply_discount(200, 50))
print(apply_discount(50, 0))
print(apply_discount(100, 100))
print(apply_discount(74.5, 20.0))
|
|