【Python如何与电脑玩石头剪刀布游戏】

1、石头剪刀布Python代码如下:

python 复制代码
import random
while True:
    a = random.randint(0, 2)
    b = int(input("请输入一个数字(0=石头, 1=剪刀, 2=布): "))
    c = ['石头', '剪刀', '布']
    if b != 0 and b != 1 and b != 2:
        print("傻子,你出错了,请输入数字0或1或2!!!")
    elif a == b:
        print("电脑出了:{}\n你出了:{}\n平手\n自动开启下一局:".format(c[a], c[b]))
        continue
    elif a == 0 and b == 2 or a == 1 and b == 0 or a == 2 and b == 1:
        print("电脑出了:{}\n你出了:{}\n你赢了!".format(c[a], c[b]))
        break
    else:
        print("电脑出了:{}\n你出了:{}\n你输了".format(c[a], c[b]))
        break

2、运行结果:

相关推荐
钱栈up28 分钟前
烧结秤校准改造:一个 0.93 系数差点让整条日报线算错数
爬虫·python·数据分析
IT空门:门主6 小时前
Spring AI 2.1.0 更新了什么:从 2.0 升上来的完整梳理
人工智能·python·spring
AI创界者7 小时前
Python 进阶实战(七十章):遗留系统重构、渐进式绞杀与技术债治理实战
人工智能·python·aigc
ZhengEnCi8 小时前
电脑操作 Agent 一直点不中:截图没更新时,提示词该怎么改才能停止瞎试
人工智能·python·agent
Wellijohn9 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍
后端·python·性能优化
bufengwangluo10 小时前
爬虫和调用 API 有什么区别?为什么优先选 API
python·网络爬虫·ip·api调用
Dmz丶10 小时前
前端转 AI 100 天 Day 54:RAG 项目实战(三)——多轮对话 + 会话管理
python
console.log('npc')11 小时前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习
geovindu12 小时前
python:Logistic Regression
开发语言·后端·python·算法