我参加了一门数据科学课程,我正在努力解决一些编程问题,我已经很长时间没有使用Python了,但是我正在努力提高我对这门语言的了解。
我的问题是:

def find_slope(x1, y1, x2, y2):
if (x1) == (x2):
return "inf"
else:
return ((float)(y2-y1)/(x2-x1))这是我的司机代码:
x1 = 1
y1 = 2
x2 = -7
y2 = -2
print(find_slope(x1, y1, x2, y2))这是我的输出:
0.5我不知道如何以正确的格式获得它,例如(((1, 2), .5), (3, 4))
注意:我为驱动程序编写了代码。
发布于 2019-05-19 14:43:13
你可以这样做:
def find_slope(input):
x1 = input[0][0]
y1 = input[0][1]
x2 = input[1][0]
y2 = input[1][1]
if (x1) == (x2):
slope = "inf"
else:
slope = ((float)(y2-y1)/(x2-x1))
output = (((x1, y1), slope), (x2, y2))
return output我更改了输入以与屏幕截图中提供的输入格式相匹配。
现在输入是一个元组,包含两个元组。每个内部元组都包含一个x坐标和一个y坐标。
您可以使用
input = ((1, 2), (-7, -2))
output = find_slope(input)输出将采用((A, slope), B)格式,其中A和B是包含x和y和弦的元组。
https://stackoverflow.com/questions/56208680
复制相似问题