Skip to content

Instantly share code, notes, and snippets.

@yuyueugene84
Last active April 18, 2024 18:03
Show Gist options
  • Save yuyueugene84/6a79ab6ef9b1dbb88328240c2b5b1678 to your computer and use it in GitHub Desktop.
Save yuyueugene84/6a79ab6ef9b1dbb88328240c2b5b1678 to your computer and use it in GitHub Desktop.
台大 111-2 Python程式設計與實務應用 作業二解答
from random import randint
print("-------歡迎來到剪刀石頭布!-------")
name = input("請輸入您的名稱:")
# 記錄玩家勝利、電腦勝利、和平手的次數
user_score = 0
comp_score = 0
draw = 0
# 將出拳、判斷輸贏重複執行三次
for i in range(1, 4):
# 玩家出拳
user_hand = int(input("請出拳 (1) 剪刀 (2) 石頭 (3) 布:"))
# 電腦出拳
comp_hand = randint(1,3)
# 判斷玩家是贏還是輸
if user_hand == comp_hand:
print("平手!")
draw += 1
elif user_hand == 1 and comp_hand == 3:
print("你贏了!")
user_score += 1
elif user_hand == 2 and comp_hand == 1:
print("你贏了!")
user_score += 1
elif user_hand == 3 and comp_hand == 2:
print("你贏了!")
user_score += 1
else:
print("你輸了!")
comp_score += 1
# 將玩家的數字轉換成文字
if user_hand == 1:
user_hand = "剪刀"
elif user_hand == 2:
user_hand = "石頭"
elif user_hand == 3:
user_hand = "布"
# 將電腦的數字轉換成文字
if comp_hand == 1:
comp_hand = "剪刀"
elif comp_hand == 2:
comp_hand = "石頭"
elif comp_hand == 3:
comp_hand = "布"
# 輸出結果
print("==================================================================")
print(name, "出了: ", user_hand, ", 電腦出了: ", comp_hand)
print("==================================================================")
print(name + " 分數: ", user_score)
print("電腦分數: ", comp_score)
print("平手: ", draw)
print("==================================================================")
print("感謝你玩剪刀石頭布!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment