亞東科技大學 電機系 施勢帆老師的教學網站

 找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 51|回復: 0

11-5-1

[複製鏈接]

73

主題

73

帖子

347

積分

管理員

Rank: 9Rank: 9Rank: 9

積分
347
發表於 6 天前 | 顯示全部樓層 |閱讀模式
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))

回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|亞東科技大學 電機系 施勢帆老師的教學網站

GMT+8, 2026-11-11 23:19 , Processed in 0.063850 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表