鱼C论坛

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

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

[复制链接]
发表于 2025-3-24 10:27:57 | 显示全部楼层
import random

choices = ['石头', '剪刀', '布']

while True:
    user_choice = input('请输入你的选择(石头/剪刀/布,输入q退出):')
    if user_choice == 'q':
        break
    if user_choice not in choices:
        print('输入无效,请重新输入。')
        continue
    computer_choice = random.choice(choices)
    print(f'你选择了:{user_choice},电脑选择了:{computer_choice}')
    if user_choice == computer_choice:
        print('平局!')
    elif (user_choice == '石头' and computer_choice == '剪刀') or (user_choice == '剪刀' and computer_choice == '布') or (user_choice == '布' and computer_choice == '石头'):
        print('你赢了!')
    else:
        print('你输了!')
这段代码通过一个无限循环让玩家可以持续进行游戏,直到输入 'q' 退出。每次游戏时,玩家输入自己的选择,程序随机生成电脑的选择,然后根据规则判断胜负并输出结果。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-24 01:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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