總網頁瀏覽量

【 Python學習 】剪刀石頭布程式 玩家與電腦

import random

player = None
computer = None
running = True
options = ("剪刀", "石頭", "")

while running:
while True:
player = input("請輸入剪刀、石頭、布:")
if player not in options:
print("input error, try again")
else:
break
# 電腦隨機選擇
computer = random.choice(options)
print(f"player:{player}, computer:{computer}")

# 判斷輸贏
if player == computer:
print("平手")
elif player == "剪刀" and computer == "":
print("玩家勝利")
elif player == "石頭" and computer == "剪刀":
print("玩家勝利")
elif player == "" and computer == "石頭":
print("玩家勝利")
else:
print("電腦勝利")
player_again = input("再玩一局?(y/n)").lower()
if not player_again == "y":
running = False

print("謝謝遊玩")

沒有留言:

張貼留言

2026大墩文化中心書法藝術節─「墨映秋月」中秋書法徵件活動

2026大墩文化中心書法藝術節─「墨映秋月」中秋書法徵件活動  活動日期: 2026/9/15 - 2026/10/15  活動時間: 徵件期間7月28日至8月11日;頒獎典禮9月20日;作品展覽9月15日至10月15日