Python实现的石头剪子布代码分享,python石头剪子布
Python实现的石头剪子布代码分享,python石头剪子布
我之前写过一篇基于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 '电脑胜'
这个没有去uli熬过大
# -*- coding:utf-8 -*-
#石头剪刀布
#石头 = 1 布 = 2 剪子 = 3
import random
class game:
def __init__(self):
self.player=1
self.comp=1
self.label=[u"石头",u"布",u"剪刀"]
pass
def run(self):
while(True):
self.player=self.player_input()
self.comp=self.comp_input()
print "player %s" %(self.label[self.player-1])
print "computer %s" %(self.label[self.comp-1])
self.iswin(self.player,self.comp)
pass
def iswin(self,player,comp):
if player==comp:
print "Tie"
return
elif player>comp:
if player==3 and comp==1:
print "you lose"
return
else:
print "you win"
return
else:
if player==1 and comp==3:
print "you win"
return
else:
print "you lose"
return
def player_input(self):
while(1):
try:
player=int(raw_input("Input 1 or 2 or 3:\n"))
if player>=1 and player<=3:
return player
except:
print "Input Error"
def comp_input(self):
return random.randint(1,3)
if __name__=="__main__":
newgame=game()
newgame.run()
相关内容
- Python解释执行原理分析,python解释原理
- Python实现远程调用MetaSploit的方法,pythonmetasploit
- Python实现Linux下守护进程的编写方法,python守护进程
- Python中replace方法实例分析,pythonreplace
- 零基础学Python(一)Python环境安装,基础学python环境
- python字符串连接方式汇总,python字符串汇总
- Python 不同对象比较大小示例探讨,python大小示例探讨
- Python中dictionary items()系列函数的用法实例,pythondictio
- Python中除法使用的注意事项,python除法
- python生成日历实例解析,python日历实例
评论关闭