如何写代码通过测试的程序?

2015-03-20 12:19:30 +08:00
 channingc
大家好,我刚刚开始学Python,请各位前辈帮帮忙,谢谢!!
这是我的Python作业,要求写代码通过老师给的测试文件才算成功。我做了半天,写了下面的代码,但是仍然通过不了测试程序。所以向大家求救一下……

这是题目:

#文件名是dice.py
"""这是一个关于投掷骰子的程序,一个骰子有六面,每面有1-6其中一个数字, 但是也允许有nsides(n面)的骰子"""

import random

def roll(nsides=6):
"""投掷一个nsides的骰子,返回一个 1到nsides的整数."""
nsides = int(nsides)
r = random.randint(0, nsides)
return r



def rollpair(nsides=6):
"""投掷一对nsides的骰子. 返回一对骰子,要以tuple的形式返回, 像(3, 6)."""

r1 = roll()
r2 = roll()
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())
return rlist



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())
return rplist



def dice_sum(pair):
""""返回一对骰子的值的总和.

>>> pair = (6, 1)
>>> dice_sum(pair)
7
"""

x, y = int()
pair [x, y]
s = x + y
return s



if __name__ == '__main__':
import doctest
doctest.testmod()

-----------------------------------------------------------------------------

下面是要通过的测试文件:

>>> 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

----------------------------------------------------------------------------------------
上面是我到目前为止写的roll, rollpair, rolls, rollpairs,sum这几个程序,但是通过不了测试,。我不明白那个随机数的顺序要怎样才和测试的程序一样。
这是错误信息:
File "tests/testA_dice.txt", line 9, in testA_dice.txt
Failed example:
rolls(ntimes=6)
Expected:
[2, 6, 6, 2, 6, 4]
Got:
[1, 5, 6, 5, 1, 5]

-----------------------------------------------------------------------------------------
还有sum里面的代码也有错误:
>>> def dice_sum(pair):
x, y = int()
pair [x, y]
s = int()
s = x + y
return s

>>> pair = (6, 1)
>>> dice_sum(pair)
Traceback (most recent call last):
File "<pyshell#108>", line 1, in <module>
dice_sum(pair)
File "<pyshell#106>", line 2, in dice_sum
x, y = int()
TypeError: 'int' object is not iterable

请问各位前辈怎么改?? 还有4天就要交作业了,这只是五个作业中的第一个,这个通过不了,后面的就别提了。请帮帮小弟,万分感谢!
3505 次点击
所在节点    Python
22 条回复
vboar
2015-03-21 11:39:22 +08:00
我们的概率论数理统计和Python结合在一起了...然后老师让我们自学= =
channingc
2015-03-21 12:27:51 +08:00
谢谢大家的指点,我已经完成了第一个文件,刚刚也完成了第二个文件,但是现在我卡在了第三个文件,不知道怎么改,请大家指点指点,谢谢……

#文件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.py

from 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)]

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/178212

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX