Last active
July 4, 2024 23:57
-
-
Save yuyueugene84/be95daeae6e0812fdb51e57137932778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
print("-------歡迎來到剪刀石頭布!-------") | |
name = input("請輸入您的名稱:") | |
win = 0 | |
lose = 0 | |
draw = 0 | |
for i in range(1, 4): | |
# 玩家出拳,防呆機制 | |
while True: | |
user_hand = input("請出拳 (1) 剪刀 (2) 石頭 (3) 布:") | |
# 如果玩家輸入的是 1, 2, 或 3 | |
if user_hand == "1" or user_hand == "2" or user_hand == "3": | |
# 將資料從字串轉換成成整數 | |
user_hand = int(user_hand) | |
# 跳出 while loop | |
break | |
else: | |
print("請輸入合法的選項~啾咪!") | |
# 電腦出拳 | |
comp_hand = random.randint(1,3) | |
# 判斷玩家是贏還是輸 | |
if user_hand == comp_hand: | |
print("平手!") | |
draw += 1 | |
elif user_hand == 1 and comp_hand == 3: | |
print("你贏了!") | |
win += 1 | |
elif user_hand == 2 and comp_hand == 1: | |
print("你贏了!") | |
win += 1 | |
elif user_hand == 3 and comp_hand == 2: | |
print("你贏了!") | |
win += 1 | |
else: | |
print("你輸了!") | |
lose += 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("==================================================================") | |
# 判斷最終勝利者 | |
if win > lose: | |
print(name, "恭喜你是最終勝利者!") | |
elif win == 1 and lose == 1 and draw == 1: | |
print("本局平手!") | |
elif draw == 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