[Python] if 、for 、陣列 綜合練習題

文、意如

可以挑戰看看這些題目,但請記得,先自己思考,
試著寫出程式碼。 當你真的想不出解法,或者想檢查自己的答案時,再對照解答。
1.學生成績分析

目標: 假設你有一組學生的姓名和成績資料,存儲在一個列表中。請寫一個程式,執行以下任務:

  1. 找出並列印出所有成績高於 85 分的學生姓名。
  2. 計算所有學生的平均分數。
  3. 找出並列印出成績最高的學生姓名和分數。
資料:
students_data = [
    {"name": "林小明", "score": 92},
    {"name": "王大華", "score": 78},
    {"name": "陳美玲", "score": 88},
    {"name": "張立偉", "score": 75},
    {"name": "黃雅婷", "score": 95},
    {"name": "吳俊傑", "score": 80}
]
解答:
students_data = [
    {"name": "林小明", "score": 92},
    {"name": "王大華", "score": 78},
    {"name": "陳美玲", "score": 88},
    {"name": "張立偉", "score": 75},
    {"name": "黃雅婷", "score": 95},
    {"name": "吳俊傑", "score": 80}
]

# 任務 1: 找出高分學生
print("--- 成績高於 85 分的學生 ---")
for student in students_data:
    if student["score"] > 85:
        print(student["name"], ":", student["score"], "分")

# 任務 2: 計算平均分數
total_score = 0
for student in students_data:
    total_score += student["score"]
average_score = total_score / len(students_data)
print(f"\n學生的平均分數為:{average_score:.2f} 分")

# 任務 3: 找出最高分學生
highest_score = 0
highest_scorer = ""
for student in students_data:
    if student["score"] > highest_score:
        highest_score = student["score"]
        highest_scorer = student["name"]

print(f"\n成績最高的學生是 {highest_scorer},分數為 {highest_score} 分")

 

2.購物清單處理

 

目標: 你有一份購物清單,包含商品名稱和價格。請寫一個程式,執行以下任務:

  1. 計算所有商品的總價格。
  2. 列印出價格超過 500 元的商品。
  3. 如果購物車總額超過 1500 元,提供 9 折優惠,並計算折扣後總價。
資料:
shopping_list = [
    {"item": "筆記型電腦", "price": 25000},
    {"item": "鍵盤", "price": 800},
    {"item": "滑鼠", "price": 450},
    {"item": "耳機", "price": 1200},
    {"item": "手機殼", "price": 300},
    {"item": "螢幕", "price": 5500}
]
參考解答:

 

shopping_list = [
    {"item": "筆記型電腦", "price": 25000},
    {"item": "鍵盤", "price": 800},
    {"item": "滑鼠", "price": 450},
    {"item": "耳機", "price": 1200},
    {"item": "手機殼", "price": 300},
    {"item": "螢幕", "price": 5500}
]

# 任務 1: 計算總價格
total_price = 0
for item in shopping_list:
    total_price += item["price"]

print(f"所有商品的總價格為:{total_price} 元")

# 任務 2: 列印出價格超過 500 元的商品
print("\n--- 價格超過 500 元的商品 ---")
for item in shopping_list:
    if item["price"] > 500:
        print(item["item"], ":", item["price"], "元")

# 任務 3: 檢查折扣並計算最終價格
if total_price > 1500:
    discounted_price = total_price * 0.9
    print(f"\n恭喜你!總金額超過 1500 元,獲得九折優惠。")
    print(f"折扣後總價格為:{discounted_price:.2f} 元")
else:
    print("\n總金額未滿 1500 元,無折扣。")
3.商品庫存檢查

目標: 你有一份商品庫存列表,每個商品都包含名稱、庫存數量和價格。請寫一個程式,執行以下任務:

  1. 列出所有庫存少於 5 個的商品。
  2. 計算所有商品的總價值(總價值 = 數量 * 價格 的總和)。
  3. 如果發現有庫存為 0 的商品,列印出提醒訊息:「OOO 商品已售罄,請及時補貨。」
資料:
inventory = [
    {"name": "咖啡豆", "stock": 10, "price": 350},
    {"name": "濾紙", "stock": 2, "price": 50},
    {"name": "馬克杯", "stock": 0, "price": 120},
    {"name": "手沖壺", "stock": 7, "price": 850},
    {"name": "磨豆機", "stock": 1, "price": 1500}
]
參考解答:
inventory = [
    {"name": "咖啡豆", "stock": 10, "price": 350},
    {"name": "濾紙", "stock": 2, "price": 50},
    {"name": "馬克杯", "stock": 0, "price": 120},
    {"name": "手沖壺", "stock": 7, "price": 850},
    {"name": "磨豆機", "stock": 1, "price": 1500}
]

# 任務 1: 列出低庫存商品
print("--- 庫存警報 (少於 5 個) ---")
for product in inventory:
    if product["stock"] < 5 and product["stock"] > 0:
        print(f"商品:{product['name']},庫存:{product['stock']} 個")

# 任務 2: 計算所有商品的總價值
total_value = 0
for product in inventory:
    total_value += product["stock"] * product["price"]

print(f"\n所有商品的總價值為:{total_value} 元")

# 任務 3: 檢查並提醒售罄商品
print("\n--- 補貨提醒 ---")
for product in inventory:
    if product["stock"] == 0:
        print(f"{product['name']} 商品已售罄,請及時補貨。")

4.電影評分篩選

目標: 你有一份電影列表,每部電影包含名稱、年份和評分。請寫一個程式,執行以下任務:

  1. 篩選出所有評分超過 8.5 分的電影,並將它們的名稱存入一個新列表 high_rated_movies
  2. 列出所有在 2000 年前上映的電影名稱。
  3. 計算所有電影的平均評分。
資料:
movies = [
    {"title": "神隱少女", "year": 2001, "rating": 8.6},
    {"title": "阿凡達", "year": 2009, "rating": 7.8},
    {"title": "鐵達尼號", "year": 1997, "rating": 7.9},
    {"title": "全面啟動", "year": 2010, "rating": 8.8},
    {"title": "魔戒三部曲:王者再臨", "year": 2003, "rating": 9.0},
    {"title": "駭客任務", "year": 1999, "rating": 8.7}
]
參考解答:
movies = [
    {"title": "神隱少女", "year": 2001, "rating": 8.6},
    {"title": "阿凡達", "year": 2009, "rating": 7.8},
    {"title": "鐵達尼號", "year": 1997, "rating": 7.9},
    {"title": "全面啟動", "year": 2010, "rating": 8.8},
    {"title": "魔戒三部曲:王者再臨", "year": 2003, "rating": 9.0},
    {"title": "駭客任務", "year": 1999, "rating": 8.7}
]

# 任務 1: 篩選高評分電影
high_rated_movies = []
for movie in movies:
    if movie["rating"] > 8.5:
        high_rated_movies.append(movie["title"])

print("--- 高評分電影 (超過 8.5 分) ---")
print(high_rated_movies)

# 任務 2: 列出 2000 年前上映的電影
print("\n--- 2000 年前上映的電影 ---")
for movie in movies:
    if movie["year"] < 2000:
        print(movie["title"])

# 任務 3: 計算平均評分
total_rating = 0
for movie in movies:
    total_rating += movie["rating"]

average_rating = total_rating / len(movies)
print(f"\n所有電影的平均評分為:{average_rating:.2f} 分")

Yiru@Studio - 關於我 - 意如