randrange.py源代码分析

 

import random

#导入随机数工具包

def rollDice():

die1 = random.randrange( 1, 7 )

#取1到7之间的任意一个数

die2 = random.randrange( 1, 7 )

#取1到7之间的任意一个数

workSum = die1 + die2

#求两个随机数的和

print "Player rolled %d + %d = %d" % ( die1, die2, workSum )

#打印和式

return workSum

#返回随机数的和

sum = rollDice() # first dice roll

if sum == 7 or sum == 11: # win on first roll

gameStatus = "WON"

elif sum == 2 or sum == 3 or sum == 12: # lose on first roll

gameStatus = "LOST"

else: # remember point

gameStatus = "CONTINUE"

myPoint = sum

print "Point is", myPoint

while gameStatus == "CONTINUE": # keep rolling

sum = rollDice()

if sum == myPoint: # win by making point

gameStatus = "WON"

elif sum == 7: # lose by rolling 7:

gameStatus = "LOST"

if gameStatus == "WON":

print "Player wins"

else:

print "Player loses"

你可能感兴趣的:(源代码)