📘 Week 6:綜合應用(進階)

重點:模組化設計、功能拆解、資料流追蹤
練習:分析資料流與模組間互動

Day 36:模組初始化與資料結構設計

users = {}
products = {}
cart = []
orders = []
def init_system():
    global users, products, cart, orders
    users = {}
    products = {}
    cart = []
    orders = []

init_system()
def reset_cart():
    global cart
    cart = []

reset_cart()
def reset_orders():
    global orders
    orders = []

reset_orders()

Day 37:模組間資料流追蹤

def register(username, password):
    users[username] = password

def add_product(name, price):
    products[name] = price

def add_to_cart(item):
    cart.append(item)
def create_order(user):
    orders.append({"user": user, "items": list(cart)})
    reset_cart()

register("會安", "1234")
add_product("蘋果", 30)
add_to_cart("蘋果")
create_order("會安")
def show_order_flow():
    print("使用者:", orders[-1]["user"])
    print("商品:", orders[-1]["items"])

show_order_flow()
def get_order_total(order):
    return sum([products[item] for item in order["items"]])

print("訂單金額:", get_order_total(orders[-1]))

Day 38:模組封裝與重構

def user_module():
    def register(username, password):
        users[username] = password
    def login(username, password):
        return users.get(username) == password
    return register, login

register, login = user_module()
register("小明", "abcd")
print(login("小明", "abcd"))
def product_module():
    def add(name, price):
        products[name] = price
    def list_all():
        return products
    return add, list_all

add_product, list_products = product_module()
add_product("芒果", 40)
print(list_products())
def cart_module():
    def add(item):
        cart.append(item)
    def clear():
        cart.clear()
    return add, clear

add_to_cart, clear_cart = cart_module()
add_to_cart("芒果")
print(cart)
def order_module():
    def create(user):
        orders.append({"user": user, "items": list(cart)})
        cart.clear()
    return create

create_order = order_module()
create_order("小明")
print(orders)

Day 39:模組測試與驗證

def test_register():
    register("測試者", "test")
    assert users["測試者"] == "test"
    print("註冊測試通過")
def test_add_product():
    add_product("測試品", 99)
    assert products["測試品"] == 99
    print("商品新增測試通過")
def test_cart_flow():
    add_to_cart("測試品")
    assert "測試品" in cart
    print("購物車流程測試通過")
def test_order_creation():
    create_order("測試者")
    assert orders[-1]["user"] == "測試者"
    print("訂單建立測試通過")

Day 40:模組整合與主程式入口

def main():
    register("會安", "1234")
    add_product("蘋果", 30)
    add_to_cart("蘋果")
    create_order("會安")
    show_orders()

main()
def show_orders():
    for order in orders:
        print(order["user"], "訂購了", order["items"])
if __name__ == "__main__":
    main()

Day 41:錯誤處理與例外機制

def safe_divide(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        return "除以零錯誤"

print(safe_divide(10, 0))
def get_price(product):
    try:
        return products[product]
    except KeyError:
        return "查無商品"

print(get_price("鳳梨"))
def read_file(filename):
    try:
        with open(filename, "r") as f:
            return f.read()
    except FileNotFoundError:
        return "檔案不存在"
def convert_to_int(value):
    try:
        return int(value)
    except ValueError:
        return "轉換失敗"

print(convert_to_int("abc"))

Day 42:模組化專案結構設計

# user_module.py
users = {}

def register(username, password):
    users[username] = password

def login(username, password):
    return users.get(username) == password
# product_module.py
products = {}

def add_product(name, price):
    products[name] = price

def get_price(name):
    return products.get(name, 0)
# cart_module.py
cart = []

def add_to_cart(item):
    cart.append(item)

def clear_cart():
    cart.clear()
# main.py
from user_module import register, login
from product_module import add_product
from cart_module import add_to_cart

register("會安", "1234")
add_product("蘋果", 30)
add_to_cart("蘋果")