python-3.x - 如何求斜率:输出格式 ((A,slope), B)
问题描述
我参加了数据科学课程,我正在尝试解决一些编程问题,我已经很长时间没有使用 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))
注意:我为驱动程序编写了代码。
解决方案
你可以这样做:
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
我更改了输入以匹配屏幕截图中给出的输入格式。
现在输入是一个元组,包含两个元组。每个内部元组都包含 ax 坐标和 ay 坐标。
您可以使用调用该函数
input = ((1, 2), (-7, -2))
output = find_slope(input)
输出格式为((A, slope), B)
,其中 A 和 B 是包含 x 和 y 坐标的元组。
推荐阅读
- ios - 如何修复我的构建:具有 CocoaPods 依赖项和 2 个目标(iOS 和 tvOS)的 .framework 文件
- node.js - 为什么我的 MongoDB 集合返回一个空数组
- discord.js - discord.js 从 fs.readdir 中获取数组
- c# - 检索每个用户对特定谷歌文档所做的更改
- angular - 角度代理配置
- php - 未在 Safari 13 中为基域设置 Laravel 6 会话 cookie
- node.js - 使用 Express 将上传的文件作为流获取
- r - 如何在 Case_When 语句中忽略字符串中的转义?
- html - 如何使一个边框不与另一个边框重叠,多选还是单选?
- sql - SQL:尝试将数据插入有关系时出现 KeyError