Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python未将值返回到函数中的变量_Python_Function_Math_Output_Algebra - Fatal编程技术网

Python未将值返回到函数中的变量

Python未将值返回到函数中的变量,python,function,math,output,algebra,Python,Function,Math,Output,Algebra,我写的这段代码很难输出两点之间的斜率和距离 在python visualizer中查看它,它似乎能够计算值,但是,距离变量并没有保存它的值。它将被坡度的值覆盖 我很难理解我应该如何在函数定义中使用return,因为这似乎是个问题 def equation(x,y,x1,y1): distance=math.sqrt(((x-x1)**2)+((y-y1)**2)) if x!=x1 and y1!=y: slope=(y1-y)/(x1-x) return slope

我写的这段代码很难输出两点之间的斜率和距离

在python visualizer中查看它,它似乎能够计算值,但是,距离变量并没有保存它的值。它将被坡度的值覆盖

我很难理解我应该如何在函数定义中使用return,因为这似乎是个问题

def equation(x,y,x1,y1):
  distance=math.sqrt(((x-x1)**2)+((y-y1)**2))
  if x!=x1 and y1!=y:
    slope=(y1-y)/(x1-x)
    return slope
  else:
    slope='null'
    return slope
  return distance
slope=equation(1,3,2,1)
print(slope)
distance=equation(1,3,2,1)
print(distance)

这里的代码输出对于这两个变量都是相同的。

返回语句在函数中遇到时退出函数。从函数返回一个元组

def equation(x,y,x1,y1):
    # calculate slope and distance
    return slope, distance

slope,distance = equation(1,3,2,1)
print(slope)
print(distance)

如果您希望两者都是不同的函数调用,即
slope=方程(1,3,2,1)
distance=方程(1,3,2,1)
,则尝试第一种方法,如果您希望两种方法都在单行中调用,即
slope,distance=方程(1,3,2,1)
,则尝试第二种方法:

第一种方法

import math
def equation(x,y,x1,y1,var):
  if var == "slope":
    if x!=x1 and y1!=y:
      slope=(y1-y)/(x1-x)
      return slope
    else:
      slope='null'
      return slope
  elif var == "distance":
    distance=math.sqrt(((x-x1)**2)+((y-y1)**2))
    return distance
slope=equation(1,3,2,1,"slope")
print(slope)
distance=equation(1,3,2,1,"distance")
print(distance)
def equation(x,y,x1,y1):
  distance=math.sqrt(((x-x1)**2)+((y-y1)**2))
  if x!=x1 and y1!=y:
    slope=(y1-y)/(x1-x)
    return slope,distance
  else:
    slope='null'
    return slope,distance
slope, distance=equation(1,3,2,1)
print(distance)
print(slope)
第二种方法

import math
def equation(x,y,x1,y1,var):
  if var == "slope":
    if x!=x1 and y1!=y:
      slope=(y1-y)/(x1-x)
      return slope
    else:
      slope='null'
      return slope
  elif var == "distance":
    distance=math.sqrt(((x-x1)**2)+((y-y1)**2))
    return distance
slope=equation(1,3,2,1,"slope")
print(slope)
distance=equation(1,3,2,1,"distance")
print(distance)
def equation(x,y,x1,y1):
  distance=math.sqrt(((x-x1)**2)+((y-y1)**2))
  if x!=x1 and y1!=y:
    slope=(y1-y)/(x1-x)
    return slope,distance
  else:
    slope='null'
    return slope,distance
slope, distance=equation(1,3,2,1)
print(distance)
print(slope)

return
语句的
distance
将永远无法到达。因为
if-else
的两条路径都返回,代码永远不会有机会到达
距离。可能您想返回一个元组而不是标量?这里距离永远不会返回,因为在if和else中,您的和“if”并不每次都匹配它转到else,因此每次将返回斜率空值,因此您可以使用两个不同的方程,一个用于斜率,一个用于距离,或者您可以返回tuple@PWier检查解决方案谢谢,这非常有帮助。我很欣赏您列出的两种方法,它们帮助我更好地理解代码。