Loop Games & Challenges
用 for 和 while 寫出好玩的互動程式!
電腦隨機選一個 1~100 的數字,你來猜!會告訴你太大還是太小。
學習重點:while True、break、if/elif/else
import random
answer = random.randint(1, 100)
count = 0
print("🎯 我想了一個 1~100 的數字,猜猜看!")
while True:
guess = int(input("你猜:"))
count += 1
if guess > answer:
print("太大了!再小一點 ⬇️")
elif guess < answer:
print("太小了!再大一點 ⬆️")
else:
print(f"🎉 答對了!答案是 {answer},你猜了 {count} 次")
break
隨機出 5 題乘法,算算你能答對幾題!
學習重點:for range()、random、計分器
import random
score = 0
total = 5
print("📝 九九乘法大挑戰!共 5 題\n")
for i in range(1, total + 1):
a = random.randint(2, 9)
b = random.randint(1, 9)
ans = int(input(f"第 {i} 題:{a} × {b} = "))
if ans == a * b:
print("✅ 正確!")
score += 1
else:
print(f"❌ 答案是 {a * b}")
print(f"\n成績:{score}/{total} 🏆")
輸入高度,畫出漂亮的金字塔!
學習重點:for、字串乘法 "*" * n
height = int(input("🌟 你想要多高的金字塔?(1~20):"))
for i in range(1, height + 1):
spaces = " " * (height - i)
stars = "★ " * i
print(spaces + stars)
print("\n🏛️ 你的金字塔完成了!")
試試改成倒過來的金字塔?或是用不同符號?
寶箱有密碼鎖!你有 5 次機會,每次會告訴你猜對幾個位置。
學習重點:while、zip()、倒數計數
password = "open"
chances = 5
print("🔒 你發現了一個寶箱,需要輸入密碼!")
print(f"提示:4 個英文字母,你有 {chances} 次機會")
while chances > 0:
guess = input(f"輸入密碼(剩 {chances} 次):")
if guess == password:
print("🎉 寶箱打開了!裡面有 100 金幣!")
break
else:
chances -= 1
correct = sum(1 for a, b in zip(guess, password) if a == b)
if chances > 0:
print(f"❌ 錯誤!但你猜對了 {correct} 個位置\n")
else:
print(f"💀 機會用完了!密碼是 {password}")
你 vs 電腦,每回合擲骰子比大小,先到 20 分獲勝!
學習重點:while 多條件、random、計分
import random
my_score = 0
pc_score = 0
rounds = 0
print("🎲 骰子大戰!先到 20 分獲勝!\n")
while my_score < 20 and pc_score < 20:
input("按 Enter 擲骰子...")
rounds += 1
my_dice = random.randint(1, 6)
pc_dice = random.randint(1, 6)
print(f"第 {rounds} 回合:你 🎲{my_dice} vs 電腦 🎲{pc_dice}", end=" ")
if my_dice > pc_dice:
my_score += my_dice
print("→ 你贏!")
elif pc_dice > my_dice:
pc_score += pc_dice
print("→ 電腦贏!")
else:
print("→ 平手!")
print(f" 比分:你 {my_score} : {pc_score} 電腦\n")
if my_score >= 20:
print("🏆 恭喜你贏了!!")
else:
print("😢 電腦贏了,再來一次!")
炸彈即將爆炸!在時間內剪對電線才能拆除!
學習重點:while、random.choice()、清單
import random
wire_colors = ["紅", "藍", "綠", "黃"]
correct_wire = random.choice(wire_colors)
timer = 5
print("💣 炸彈倒數計時!剪對電線才能拆除!")
print(f"電線顏色:{', '.join(wire_colors)}\n")
while timer > 0:
print(f"⏰ 剩餘 {timer} 秒!")
choice = input("剪哪條電線?")
if choice == correct_wire:
print("🎉 炸彈拆除成功!你是英雄!")
break
else:
timer -= 1
if timer > 0:
print(f"❌ 不是這條!快再試試!\n")
else:
print(f"💥 轟!炸彈爆炸了!正確答案是「{correct_wire}」")
模擬自動販賣機!用迴圈讓玩家反覆購買商品。
學習重點:while True + for、字典、清單
menu = {"可樂": 20, "果汁": 25, "餅乾": 15, "巧克力": 30}
wallet = 100
bag = []
print("🏪 歡迎光臨自動販賣機!")
print(f"💰 你有 {wallet} 元\n")
while True:
print("--- 商品列表 ---")
for item, price in menu.items():
print(f" {item}:{price} 元")
print(" (輸入「結帳」離開)\n")
choice = input("想買什麼?")
if choice == "結帳":
break
elif choice in menu:
if wallet >= menu[choice]:
wallet -= menu[choice]
bag.append(choice)
print(f"✅ 買了 {choice}!剩餘 {wallet} 元\n")
else:
print("❌ 錢不夠了!\n")
else:
print("❓ 沒有這個商品喔\n")
print(f"\n🛍️ 你買了:{', '.join(bag) if bag else '什麼都沒買'}")
print(f"💰 剩餘:{wallet} 元")
試著修改程式碼,加入自己的創意!
例如:改變規則、加入更多選項、讓遊戲更難。