读书人

一个简单的python程序纯熟抛砖引玉,

发布时间: 2012-05-21 18:04:41 作者: rapoo

一个简单的python程序,纯熟抛砖引玉

Python code
import random,syscount=win=lose=draw=0def create():    flag=random.randint(1,3)    if flag==1:        return '石头'    elif flag==2:        return '剪刀'    else:        return '布'    def userAction():    action=int(input('石头剪刀布:\n1=石头\n2=剪刀\n3=布\n4=退出\n'))    if action==1:        return '石头'    elif action==2:        return '剪刀'    elif action==3:        return '布'    elif action==4:        print 'system out'        print 'you have played %d times,wined %d times,\draw game %d times,lose game %d times' %(count,win,draw,lose)        sys.exit()def compare(v1,v2):        global count,win,lose,draw        if v1=='石头' and v2=='石头':            print 'the same action,try agin'            draw+=1            count+=1        elif v1=='石头' and v2=='布':            print 'you win,computer lose'            win+=1            count+=1        elif v1=='石头' and v2=='剪刀':            print 'you lose,computer win'            lose+=1            count+=1        elif v1=='剪刀' and v2=='布':            print 'you lose,computer win'            lose+=1            count+=1        elif v1=='剪刀' and v2=='石头':            print 'you win,computer lose'            win+=1            count+=1        elif v1=='剪刀' and v2=='剪刀':            print 'the same action,try agin'            draw+=1            count+=1        elif v1=='布' and v2=='剪刀':            print 'you win,computer lose'            win+=1            count+=1        elif v1=='布' and v2=='石头':            print 'you lose,computer win'            lose+=1            count+=1        elif v1=='布' and v2=='布':            print 'the same action,try agin'            draw+=1            count+=1while True:    compare(create(),userAction())

输出如下:
石头剪刀布:
1=石头
2=剪刀
3=布
4=退出
2
you lose,computer win
石头剪刀布:
1=石头
2=剪刀
3=布
4=退出
4
system out
you have played 1 times,wined 0 times,draw game 0 times,lose game 1 times

[解决办法]
探讨
你真的在玩么?


Python code


def play():
message = ["You win, good job.", "Tie", "You lose, keep trying."]
counter = [0, 0, 0]
while int(input('石头剪刀布:\n1=石头\n2=剪刀\n3=布\n4=退出\n')) != 4:……

读书人网 >perl python

热点推荐