该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#coin.py
import random #coin类
#包含一个属性dictCoin,类型是字典,保存正反面
#包含一个方法getCoin,获取本次硬币是哪一面
class coin:
dictCoin = {
'1':'front',
'2':'back'
}
def getCoin(self):
c = random.randint(1,2)
sc = str(c)
print sc
return self.dictCoin[sc]
#player.py
import coin
#包含属性co, fco, bco记录本次抛硬币的次数和结果
#包含属性count, fcount, bcount记录总次数和总结果
#包含方法do,抛硬币
class player:
def __init__(self):
self.count = 0
self.c = coin.coin()
self.fcount = 0
self.bcount = 0
def do(self):
co = int(raw_input('enter count:'))
i = 0
fco = 0
bco = 0
while i < co:
i += 1
if self.c.getCoin() == 'front':
fco += 1
else:
bco += 1
print 'this time, count:%r, front:%r, back:%r'\
% (co, fco, bco)
self.count += co
self.fcount += fco
self.bcount += bco
print 'total count:%r, front count:%r, back count:%r'\
% (self.count, self.fcount, self.bcount)
在shell界面运行的话就是
from player import *
p = player()
p.do()
如果想继续运行,那就继续输入
p.do()