Skip to content

Instantly share code, notes, and snippets.

@yuyueugene84
Last active July 5, 2024 00:00
Show Gist options
  • Save yuyueugene84/efcf923301cd3f9dcd37ecaeac157aca to your computer and use it in GitHub Desktop.
Save yuyueugene84/efcf923301cd3f9dcd37ecaeac157aca to your computer and use it in GitHub Desktop.
import random
print("-------歡迎來到剪刀石頭布!-------")
name = input("請輸入您的名稱:")
# 記錄玩家贏、輸、平手次數
scores = [0,0,0]
# 玩家出拳
for i in range(1, 4):
while True:
user_hand = input("請出拳 (1) 剪刀 (2) 石頭 (3) 布:")
if user_hand in ["1", "2", "3"]:
user_hand = int(user_hand)
break
else:
print("一定要輸入正確資料哦,啾咪!")
# 電腦出拳
comp_hand = random.randint(1,3)
# 判斷玩家是贏還是輸
if user_hand == comp_hand:
print("平手!")
scores[2] += 1
elif user_hand == 1 and comp_hand == 3:
print("你贏了!")
scores[0] += 1
elif user_hand == 2 and comp_hand == 1:
print("你贏了!")
scores[0] += 1
elif user_hand == 3 and comp_hand == 2:
print("你贏了!")
scores[0] += 1
else:
print("你輸了!")
scores[1] += 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("玩家贏的次數:", scores[0])
print("玩家輸的次數:", scores[1])
print("兩邊平手次數:", scores[2])
# 判斷最終勝利者
if scores[0] > scores[1]:
print(name, "恭喜你是最終勝利者!")
elif scores[0] == 1 and scores[1] == 1 and scores[2] == 1:
print("本局平手!")
elif scores[2] == 3:
print("本局平手!")
else:
print(name, "你輸了,哭哭 T_T")
print("感謝你玩剪刀石頭布!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment