当前位置: 移动技术网 > IT编程>脚本编程>Python > Python实现的石头剪子布代码分享

Python实现的石头剪子布代码分享

2019年03月27日  | 移动技术网IT编程  | 我要评论

创维42c08rd,宿州之窗,两女一桶

我之前写过一篇基于js的石头剪子布程序 《javascript实现的石头剪刀布游戏源码分享》,今天又基于python写了一个实例,这里边的算法有点特殊但一时也想不到一个好的算法规律。

代码:

复制代码 代码如下:

# encoding=utf-8
# 石头剪子布 程序
# 李忠
import random
 
# 定义石头剪子布字典
dict = {1:'剪子',2:'石头',3:'布'}
 
for row in dict:
    print '编号:',row,' = ',dict[row]
 
print '您出什么?'
 
loop = true
while loop:
    you = raw_input('请输入编号回车: ')
    try:
        you = int(you)
        if you>=1 and you<=3:
            loop = false
        else:
            print '请输入 1-3 范围内的编号'
    except exception,e:
        print '请输入正确的数字编号'
 
dn = random.randint(1,3)
print '你出:',dict[you]
print '电脑出:',dict[dn]
print '结果:',
 
if dn==you:
    print '平局'
elif (you>dn and you-dn==1) or you+2==dn:
    print '你胜'
else:
    print '电脑胜'

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网