如何在Python中为此斜率编写代码

时间:2018-01-13 14:52:52

标签: python

Mock_ApiClient_anyHash

我有这个写法,但每次运行它都会返回0.0

的值

1 个答案:

答案 0 :(得分:1)

  1. 我建议使用raw_input而不是input

  2. 如果使用python 2.x,如果两个参数都是/,则int为分层,因此要保存,请确保输入转换为{{1} }。

  3. 请注意float+的操作顺序:如果/之前需要评估+,则需要将其/放入()

  4. 您需要打印变量slope

  5. 所以:

    x1 = float(raw_input('x1'))  # point 1 and point 2 (conversion to float)
    x2 = float(raw_input('x2'))
    y1 = float(raw_input('y1'))
    y2 = float(raw_input('y2'))
    
    slope = (y2-y1) / (x2-x1)  # point 3: () are needed here
    
    print("The slope is {0}".format(slope))  # point 4