randrange1.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"

#若和为7或11,则获胜

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

gameStatus = "LOST"

#若和为2、3或12,则为输

else: # remember point

gameStatus = "CONTINUE"

#其他情况,继续

myPoint = sum

print "Point is", myPoint

#打印分数

while gameStatus == "CONTINUE": # keep rolling

sum = rollDice()

#若状态为"CONTINUE",则继续掷

if sum == myPoint: # win by making point

gameStatus = "WON"
#若第二次掷得的分数与第一次一样,则获胜

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

gameStatus = "LOST"

#若第二次掷得的分数为7,则为输

if gameStatus == "WON":

print "Player wins"

else:

print "Player loses"

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