免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1490 | 回复: 4
打印 上一主题 下一主题

这个题目怎么做,就后面半个题目,看不懂意思。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-08 10:03 |只看该作者 |倒序浏览
本帖最后由 drongh 于 2012-04-08 10:30 编辑

Write a function slope(x1, y1, x2, y2) that returns the slope of the line through the points (x1, y1) and (x2, y2). Be sure your implementation of slope can pass the following doctests:

def slope(x1, y1, x2, y2):
"""
>>> slope(5, 3, 4, 2)
1.0
>>> slope(1, 2, 3, 2)
0.0
>>> slope(1, 2, 3, 3)
0.5
>>> slope(2, 4, 1, 2)
2.0
"""

Then a call to slope in a new function named intercept(x1, y1, x2, y2) that returns the y-intercept of the line through the points (x1, y1) and (x2, y2).

def intercept(x1, y1, x2, y2):
"""
>>> intercept(1, 6, 3, 12)
3.0
>>> intercept(6, 1, 1, 6)
7.0
>>> intercept(4, 6, 12,
5.0
"""

intercept should pass the doctests above.


后面的半个怎么做。
下面是前半个题目的程序,已经完成。

def slope(x1,y1,x2,y2):
    """
    >>> slope(5,3,4,2)
    1.0
    >>> slope(1,2,3,2)
    0.0
    >>> slope(1,2,3,3)
    0.5
    >>> slope(2,4,1,2)
    2.0
   
    """
   
   if x1 == x2:
        print "The slope doesn't exist"
        return
    else:
        return (float(y2-y1)) / (x2-x1)


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

论坛徽章:
0
2 [报告]
发表于 2012-04-08 10:43 |只看该作者
(x1,y1), (x2, y2)这两点确定一条直线,题目的意思就是求这条直线的正切值

论坛徽章:
0
3 [报告]
发表于 2012-04-08 11:09 |只看该作者
http://openbookproject.net/thinkcs/python/english2e/ch05.html
可以到这个网站上去看这个题,最后面第三题。

上面的斜率我写出了,下面的题是什么意思。

论坛徽章:
0
4 [报告]
发表于 2012-04-08 12:42 |只看该作者
直线y=a+bx,前半题求b,后半题求a

论坛徽章:
0
5 [报告]
发表于 2012-04-08 14:26 |只看该作者
测试通过了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP