告诉你直线斜率的 Python 程序

标签 python variables

所以我是 python 的新手,但已经成功地创建了可以计算面积、体积、将摄氏度转换为华氏度等的程序……但是,我似乎在使用这个“直线斜率”程序时遇到了一些麻烦。

# A simple program which prompts the user for two points 
# and then computes and prints the corresponding slope of a line.

# slope(S): (R*R)*(R*R) -> R
# If R*R is a pair of real numbers corresponding to a point,
# then slope(S) is the slope of a line.
def x1(A):
    def y1(B):
        def x2(C):
            def y2(D):
                def slope(S):
                    return (D-B)/(C-A)

# main
# Prompts the user for a pair of points, and then computes
# and prints the corresponding slope of the line.

def main():
    A = eval(input("Enter the value of x1:"))
    B = eval(input("Enter the value of y1:"))
    C = eval(input("Enter the value of x2:"))
    D = eval(input("Enter the value of y2:"))
    S = slope(S)
    print("The slope of a line created with those points\
 is: {}{:.2f}".format(S,A,B,C,D))

main()

最佳答案

斜率函数可能类似于以下内容 - 一个采用四个参数的函数,代表这两个点的四个坐标:

def slope(x1, y1, x2, y2):
    return (y1 - y2) / (x1 - x2)

但显然不应该这么简单,还得细化一下,考虑x1 == x2的情况。

关于告诉你直线斜率的 Python 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25955365/

相关文章:

Python numpy : cannot convert datetime64[ns] to datetime64[D] (to use with Numba)

python - 更改 `wsgi.py` 后,Django-Rest-Framework 无法加载样式和脚本

sql-server - 如何在 SSIS 变量中存储 'fully qualified' 和 'name only' 文件名

c - 如何计算局部变量和用户定义函数的循环执行次数? C语言

variables - 问题设置返回文本到重复循环中的变量 - OS X 10.9.x

c - 在函数中使用#define

python - 加倍二进制数

python - 运行虚拟环境 Ansible

python - 下载YFCC-100M数据集

variables - 包含另一个变量值的变量名