Skip to main content
 首页 » 编程设计

告诉你直线斜率的 Python 程序

2025年05月04日15bluestorm

所以我是 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的情况。