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

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天就要交作业了,这只是五个作业中的第一个,这个通过不了,后面的就别提了。请帮帮小弟,万分感谢!
3427 次点击
所在节点    Python
22 条回复
cheerzeng
2015-03-20 14:08:58 +08:00
这个测试要求有问题吧,既然是随机生成的骰子,怎么可能会有一样的结果。
cheerzeng
2015-03-20 14:13:22 +08:00
x, y = int()
TypeError: 'int' object is not iterable
这个原因是,要分开实例化。
cheerzeng
2015-03-20 14:20:48 +08:00
说错了,结果的问题应该是要利用那个种子
channingc
2015-03-20 14:35:53 +08:00
@cheerzeng 那个结果我知道为什么会这样了。 r = random.randint(1, nsides) 是因为那个随机数要从1开始。我改了之后结果就一样了
channingc
2015-03-20 14:39:33 +08:00
@cheerzeng 什么是分开实例化??
cheerzeng
2015-03-20 14:46:46 +08:00
@channingc 我把你的这个从1开始了,好像也不行,哈哈。就是
x=int()
y = int()

或者
x,y = (int(), int())
init
2015-03-20 16:13:08 +08:00
现在大学开始教py了吗
roychan
2015-03-20 16:22:12 +08:00
@init 我们大一也在学 Python,交作业用 Git
刚学三周,在写迷你型 PostScript 解释器了…
init
2015-03-20 17:10:30 +08:00
@roychan 你们学校应该不错吧 哪?
roychan
2015-03-20 18:23:31 +08:00
@init 其实只是个野鸡大学…ShanghaiTech
liubiantao
2015-03-20 18:54:27 +08:00
@roychan 首届啊,你们肩负着厚望,以后学弟学妹们可是要靠你们来吹牛逼的
roychan
2015-03-20 19:39:24 +08:00
@liubiantao 巨累…都不好意思给学弟学妹推荐了
init
2015-03-20 20:35:49 +08:00
@roychan 我们只开过 c c++ c# js java。。py lua什么的都没听过。。
luw2007
2015-03-20 21:49:32 +08:00
随机数, 不能完全等于测试中的例子。
也就看能不能成功运行这些方法。
正确性, 只能是算出现的百分比是不是接近概率。
roychan
2015-03-20 22:49:33 +08:00
@init 我们现在的课对所有专业都是必修
sennes
2015-03-20 23:43:13 +08:00
我python学的不是很好哈。
1.
我试了一下跑你的rolls(ntimes=6)
和你跑出来的结果并不一样,不知道是不是和环境有关?(系统、python版本)

2.
然后我看你roll()用的是random.randint。 我试着改了下
roll()里用random.choice、random.randrange、random.randint 都试了下。
[3, 4, 1, 1, 1, 4]
[2, 2, 4, 2, 4, 3]
[2, 2, 2, 6, 6, 6]
结果都是不一样的。
所以会不会和你第一个roll() 产生骰子的方式有关系?
oott123
2015-03-21 08:11:06 +08:00
random.seed('pythonistas')
这才是重点,播种之后结果就一样了
channingc
2015-03-21 11:20:05 +08:00
@init 看选什么专业而已
channingc
2015-03-21 11:23:15 +08:00
@luw2007 原来是像oott123所说的,用了random.seed('pythonistas'),播种之后结果就一样了
channingc
2015-03-21 11:28:47 +08:00
@sennes 系统是win7 64位,Python版本是3.4.1。
原来剩下的每个程序里面还要加上nsides,例如
def rollpair(nsides=6):
r1 = roll(nsides)
r2 = roll(nsides)
r = (r1, r2)
return r

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

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

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

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

© 2021 V2EX