闲来无事,写一个小程序,练习一下python,哈哈:
#!/usr/bin/env python
#coding:utf8
#author : wangganyu188@163.com
#date : 2014-09-11
import random,sys
#定义石头剪子布字典
dict = {
1:'剪子',
2:'石头',
3:'布',
}
for row in dict:
print '编号:',row, '=',dict[row]
loop = True
while loop:
print '您出什么?'
you = raw_input('请输入编号回车:如果想退出游戏按q键').strip()
if you == 'q':
loop = False
print "友情提示:游戏将退出,迎下次再玩"
sys.exit()
else:
try:
dn = random.randint(1,3)
you = int(you)
if you >= 1 and you <= 3:
loop = True
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 '电脑胜'
continue
以上就是python小程序: 剪子,石头,布实例代码的详细内容,更多请关注php中文网其它相关文章!
……