鱼C论坛

 找回密码
 立即注册
查看: 1315|回复: 10

[已解决]石头剪刀布的代码

[复制链接]
发表于 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()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-22 13:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表