小工具知识网

当前位置: 首页 > 行业知识 > 【Python如何与电脑玩石头剪刀布游戏】

行业知识

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

2026-11-30 05:40:43 来源:互联网转载

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

import randomwhile 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、运行结果:

上一篇:怎么自建定位服务器?

下一篇:阿里云服务器漏洞怎么修复