|
发表于 2025-3-29 18:10:15
|
显示全部楼层
import random
def get_user_choice():
while True:
user_choice = input("请输入你的选择(石头、剪刀、布):").strip()
if user_choice in ["石头", "剪刀", "布"]:
return user_choice
else:
print("输入无效,请重新输入。")
def get_computer_choice():
choices = ["石头", "剪刀", "布"]
return random.choice(choices)
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "平局"
elif (
(user_choice == "石头" and computer_choice == "剪刀") or
(user_choice == "剪刀" and computer_choice == "布") or
(user_choice == "布" and computer_choice == "石头")
):
return "你赢了"
else:
return "你输了"
def play_game():
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"你选择了:{user_choice}")
print(f"电脑选择了:{computer_choice}")
result = determine_winner(user_choice, computer_choice)
print(result)
if __name__ == "__main__":
play_game() |
|