谢谢大家的指点,我已经完成了第一个文件,刚刚也完成了第二个文件,但是现在我卡在了第三个文件,不知道怎么改,请大家指点指点,谢谢……
#文件1
dice.py"""这是一个关于投掷骰子的程序,一个骰子有六面,每面有1-6其中一个数字, 但是也允许有nsides(n面)的骰子"""
import random
def roll(nsides=6):
"""投掷一个nsides的骰子,返回一个 1到nsides的整数."""
r = random.randint(1, nsides)
return r
def rollpair(nsides=6):
"""投掷一对nsides的骰子. 返回一对骰子,要以tuple的形式返回, 像(3, 6)."""
r1 = roll(nsides)
r2 = roll(nsides)
r = (r1, r2)
return r
def rolls(ntimes=10, nsides=6):
"""投掷一个nsides的骰子ntimes(n次). 返回一个list.
>>> import random; random.seed('downtown')
>>> rolls()
[2, 5, 4, 5, 4, 1, 6, 6, 2, 2]
"""
rlist = []
for n in range(ntimes):
rlist.append(roll(nsides))
return rlist
""" rolls = [random.randint(1,nsides) for i in range(ntimes)]
print(rolls)
"""
def rollpairs(ntimes=10, nsides=6):
"""投掷一对nsides的骰子ntimes. 返回一个list.
>>> import random; random.seed('pythonistas')
>>> rollpairs()
[(2, 6), (6, 2), (6, 4), (5, 5), (6, 3), (2, 4), (1, 3), (3, 4), (5, 6), (4, 5)]
"""
rplist = []
for n in range(ntimes):
rplist.append(rollpair(nsides))
return rplist
def dice_sum(pair):
""""返回一对骰子的值的总和.
>>> pair = (6, 1)
>>> dice_sum(pair)
7
"""
return sum(pair)
if __name__ == '__main__':
import doctest
doctest.testmod()
--------------------------------------------------------------------------------
测试文件: testA_dice..txt
>>> from dice import roll, rollpair, rolls, rollpairs
>>> import random
>>> random.seed('pythonistas')
>>> rolls(ntimes=6)
[2, 6, 6, 2, 6, 4]
>>> rolls(ntimes=6)
[5, 5, 6, 3, 2, 4]
Roll 600 times.
>>> rolls(ntimes=600)
[1, 3, 3, 4, 5, 6, 4, 5, 6, ..., 6, 1, 3, 2, 2]
Roll a pair of dice 20 times.
>>> rollpairs(20)
[(4, 2), (3, 2), (4, 3), (4, 4), (2, 2), (3, 3), ..., (5, 3), (6, 1), (6, 1)]
Roll a pair of 20-sided dice 10 times.
>>> rollpairs(ntimes=10, nsides=20)
[(12, 11), (8, 4), (18, 12), (20, 9), (2, 19), (4, 19), (16, 8), (16, 4), (20, 3), (7, 5)]
>>> from dice import dice_sum
>>> pair = (6, 4)
>>> dice_sum(pair)
10
------------------------------------------------------------------------------------------------
文件2
predicates.pyfrom dice import dice_sum
def is_double(pair):
"""如果一对骰子的数字是一样返回True, 如果不是一样返回False
>>> is_double((3, 3))
True
"""
if pair[0]==pair[1]:
return True
else:
return False
def is_double_six(pair):
"""如果一对骰子的数字是6就返回True, 如果不是一样6返回False """
if pair[0]==pair[1]==6:
return True
else:
return False
def is_seven_eleven(pair):
"""如果一对骰子的数字加起来等于7或者等于11返回True, 如果不是返回False.
>>> is_seven_eleven((6, 5)) and is_seven_eleven((3, 4))
True
"""
if dice_sum(pair)==7:
return True
elif dice_sum(pair)==11:
return True
else:
return False
def sum_is(pair, n):
"""如果一对筛子的数字加起来等于n返回True,不是返回 False.
>>> sum_is((4,5), 9), sum_is((3, 2), 9)
(True, False)
"""
if dice_sum(pair)==n:
return True
else:
return False
def sum_is_at_least(pair, n):
"""如果一对筛子的数字加起来大于或等于n返回True,不是返回 False。
>>> pair1, pair2 = (5, 3), (6, 3)
>>> sum_is_at_least(pair1, 8) and sum_is_at_least(pair2, 8)
True
"""
if dice_sum(pair)>=n:
return True
else:
return False
def sum_is_at_most(pair, n):
"""如果一对筛子的数字加起来小于或等于n返回True,不是返回 False. """
if dice_sum(pair)<=n:
return True
else:
return False
if __name__ == '__main__':
import doctest
doctest.testmod()
-------------------------------------------------------------------------------------------
测试文件: testB_predicates.txt
>>> from dice import rolls, rollpairs
>>> from predicates import is_double, is_double_six, is_seven_eleven
>>> import random
>>> random.seed('pythonistas')
>>> rollpairs(20)
[(2, 6), (6, 2), ..., (3, 5), (6, 3), (5, 3), (6, 3), (2, 6), (4, 2), (3, 6), (6, 5), (5, 4)]
[(4, 2), (3, 2), (4, 3), (4, 4), (2, 2), (3, 3), ..., (5, 3), (6, 1), (6, 1)]
Roll a pair of 20-sided dice 10 times.
>>> rollpairs(ntimes=10, nsides=20)
[(12, 15), (4, 15), (8, 10), (5, 12), (19, 11), (8, 9), (19, 3), (1, 17), (2, 3), (2, 4)]
>>> pair = (5, 5)
>>> is_double(pair)
True
>>> pair = (6, 5)
>>> is_double(pair)
False
>> is_seven_or_eleven(pair)
True
--------------------------------------------------------------------------------------------
文件3
simulations.py"""模拟,仿真。掷骰子掷到某一个要求为止"""
from dice import roll, rollpair, dice_sum
from predicates import is_double, is_double_six, is_seven_eleven
from predicates import sum_is, sum_is_at_least, sum_is_at_most
def rolls_until(goal):
"""掷一对骰子直到遇到goal, 然后以list的形式返回每一对骰子的数字.
>>> import random; random.seed('EV')
>>> rolls_until(is_double_six)
[(1, 2), (1, 2), (2, 6), (5, 6), (4, 4), (6, 1), (6, 2), (5, 2), (3, 1), (6, 6)]
"""
rulist = list()
while rollpair()!=goal:
rulist.append(rollpair())
rulist.append(rollpair())
return rulist
def how_many_rolls_until(goal):
""""掷一对骰子直到遇到goal,然后返回一个数是计算在这个过程中掷了多少对骰子
>>> import random; random.seed('EV')
>>> how_many_rolls_until(is_double_six)
10
"""
if __name__ == '__main__':
import doctest
doctest.testmod()
----------------------------------------------------------------------------------
测试文件3:testC_simulations.txt
>>> from simulations import rolls_until
>>> import predicates
>>> import random
>>> random.seed('pythonistas')
>>> rolls_until(predicates.is_seven_eleven)
[(2, 6), (6, 2), (6, 4), (5, 5), (6, 3), (2, 4), (1, 3), (3, 4)]
>>> myrolls = rolls_until(predicates.is_double_six)
>>> len(myrolls)
63
>>> myrolls
[(5, 6), (4, 5), (6, 4), (3, 5), (6, 3), ..., (1, 2), (3, 3), (4, 4), (6, 6)]
>>> myrolls = rolls_until(predicates.is_double)
>>> len(myrolls)
6
>>> myrolls
[(6, 3), (5, 3), (2, 1), (5, 4), (6, 2), (2, 2)]