一个简单的python程序,纯熟抛砖引玉
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())