總網頁瀏覽量

【 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("謝謝遊玩")

沒有留言:

張貼留言

2025西安碑林海峽兩岸臨書徵件辦法

簡章 報名表 臨書目錄