闲来无事,写一个小程序,练习一下python,哈哈:
#!/usr/bin/env python
#coding:utf8
#author : [email protected]
#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
时间: 2024-10-10 00:34:35