📘 Week 5:綜合應用

重點:整體架構理解
練習:撰寫功能說明文件

Day 29:會員系統 - 註冊功能

users = {}

def register(username, password):
    if username in users:
        return "使用者已存在"
    users[username] = password
    return "註冊成功"

print(register("會安", "1234"))
def show_users():
    for user in users:
        print("帳號:", user)

show_users()
def is_registered(username):
    return username in users

print(is_registered("會安"))
def get_user_count():
    return len(users)

print("目前使用者數:", get_user_count())

Day 30:會員系統 - 登入功能

def login(username, password):
    if username in users and users[username] == password:
        return "登入成功"
    return "帳號或密碼錯誤"

print(login("會安", "1234"))
def change_password(username, new_password):
    if username in users:
        users[username] = new_password
        return "密碼已更新"
    return "使用者不存在"

print(change_password("會安", "5678"))
def delete_user(username):
    if username in users:
        del users[username]
        return "帳號已刪除"
    return "使用者不存在"

print(delete_user("會安"))
print(register("小明", "abcd"))
print(login("小明", "abcd"))

Day 31:商品系統 - 商品管理

products = {}

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

add_product("蘋果", 30)
add_product("香蕉", 20)
def show_products():
    for name, price in products.items():
        print(name, "價格:", price)

show_products()
def update_price(name, new_price):
    if name in products:
        products[name] = new_price

update_price("蘋果", 35)
def delete_product(name):
    if name in products:
        del products[name]

delete_product("香蕉")

Day 32:購物車系統 - 加入與計算

cart = []

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

add_to_cart("蘋果")
add_to_cart("芒果")
def show_cart():
    for item in cart:
        print("購物車項目:", item)

show_cart()
def get_total():
    total = 0
    for item in cart:
        if item in products:
            total += products[item]
    return total

print("總金額:", get_total())
def clear_cart():
    cart.clear()

clear_cart()

Day 33:訂單系統 - 建立與查詢

orders = []

def create_order(user, items):
    orders.append({"user": user, "items": items})

create_order("小明", ["蘋果", "芒果"])
def show_orders():
    for order in orders:
        print(order["user"], "訂購了", order["items"])

show_orders()
def get_order_count():
    return len(orders)

print("訂單數:", get_order_count())
def find_orders_by_user(user):
    return [o for o in orders if o["user"] == user]

print(find_orders_by_user("小明"))

Day 34:報表系統 - 統計分析

def get_sales_summary():
    summary = {}
    for order in orders:
        for item in order["items"]:
            summary[item] = summary.get(item, 0) + 1
    return summary

print(get_sales_summary())
def get_top_item():
    summary = get_sales_summary()
    return max(summary, key=summary.get)

print("最熱門商品:", get_top_item())
def get_user_order_count(user):
    return len(find_orders_by_user(user))

print("小明的訂單數:", get_user_order_count("小明"))

Day 35:功能說明文件撰寫

def register(username, password):
    """
    註冊新使用者。
    如果使用者已存在,回傳錯誤訊息。
    """
    if username in users:
        return "使用者已存在"
    users[username] = password
    return "註冊成功"
def login(username, password):
    """
    登入功能。
    檢查帳號與密碼是否正確。
    """
    if username in users and users[username] == password:
        return "登入成功"
    return "帳號或密碼錯誤"
def add_product(name, price):
    """
    新增商品至商品清單。
    name: 商品名稱
    price: 商品價格
    """
    products[name] = price
def create_order(user, items):
    """
    建立新訂單。
    user: 使用者名稱
    items: 購買商品列表
    """
    orders.append({"user": user, "items": items})