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

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

11-3-6

[複製鏈接]

66

主題

66

帖子

314

積分

管理員

Rank: 9Rank: 9Rank: 9

積分
314
發表於 前天 15:03 | 顯示全部樓層 |閱讀模式

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))


回復

使用道具 舉報

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

本版積分規則

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

GMT+8, 2026-11-5 00:41 , Processed in 0.060725 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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