Skip to content

Instantly share code, notes, and snippets.

@yuyueugene84
Last active November 9, 2021 09:19
Show Gist options
  • Save yuyueugene84/ded27a83075769b3ef367b27ed0ddadc to your computer and use it in GitHub Desktop.
Save yuyueugene84/ded27a83075769b3ef367b27ed0ddadc to your computer and use it in GitHub Desktop.
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 + " 分數: " + str(user_score))
print("電腦分數: " + str(comp_score))
print("平手: " + str(draw))
print("==================================================================")
print("感謝你玩剪刀石頭布!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment