擅长:python、mysql、java
<p>如果希望两个函数调用都是不同的,即<code>slope=equation(1,3,2,1)</code>和<code>distance=equation(1,3,2,1)</code>,则尝试第一种方法,如果希望两个函数调用都是单行的,即<code>slope, distance=equation(1,3,2,1)</code>,则尝试第二种方法:</p>
<p><strong>第一种方法</strong></p>
<pre><code>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)
</code></pre>
<p><strong>第二种方法</strong></p>
<pre><code>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)
</code></pre>