python计算一组数据的斜率_在python中找出具有给定斜率的y-intercept

这篇博客中,作者遇到了在计算斜坡截距时遇到的问题。他们分享了一个测试用例,其中前几个测试通过了,但在最后一个测试单元上遇到了困难。他们提供了测试函数和`intercept`函数的定义,并请求帮助找出导致最后一个测试失败的错误。
摘要由CSDN通过智能技术生成

我试图计算斜坡的截距,但是我不能测试所有的测试单元。我得到的第一个测试单位工作,但最后一个我有一些麻烦。有人能帮我找到错误吗?

def test(actual, expected):

""" Compare the actual to the expected value,

and print a suitable message.

"""

import sys

linenum = sys._getframe(1).f_lineno # get the caller's line number.

if (expected == actual):

msg = "Test on line {0} passed.".format(linenum)

else:

msg = ("Test on line {0} failed. Expected '{1}', but got '{2}'."

. format(linenum, expected, actual))

print(msg)

def slope (x1, y1, x2, y2):

x2 = (x2 - x1)

y2 = (y2 - y1)

m = (y2/x2)

return m

def intercept(x1, y1, x2, y2):

m = slope(x1,y1,x2,y2)

b = y2 - (m*x2)

return b

def test_suite():

test(intercept(1, 6, 3, 12), 3.0)

test(intercept(6, 1, 1, 6), 7.0)

test(intercept(4, 6, 12, 8), 5.0)

test_suite()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值