此代码需要在一行上接受两个点,并返回等式m
和b
中y = m*x + b. (m = (y1 - y2) / (x1 - x2)
和b = y1 - (m*x1)
的值。我对此进行了编码,但我将列出所有找到的信息:(“第一点是:”,“和:”,“第二点是:”,“ m =“, “ B =“等)。我注意到从返回的打印列表中,它仅运行第一个打印,而不运行所有5个打印。我尝试为其添加\ n以在新行中编写它,但是它没有用。如何使它列出我编码的每个退货单并说出它随机选择的2点。
import sys
import sys
import math
import random
number = random.randint(1,100)
number2 = random.randint(1,100)
number3 = random.randint(1,100)
number4 = random.randint(1,100)
def line_equation(x2,y2,x1,y1):
rise = y2-y1
run = x2-x1
m = rise/run
b = y2/(m*x2)
return print("m = " + str(m) + " and b = " + str(b))
return print("the first point is: ",number)
return print("and: ",number2)
return print("the second point is: ",number3)
return print("and: ",number4)
line_equation(number, number2, number3, number4)
答案 0 :(得分:3)
“ return”将按字面意义返回给调用者(因此离开该函数)。这就是为什么其余行不打印的原因。
这应该做您想要的。
def line_equation(x2,y2,x1,y1):
rise = y2-y1
run = x2-x1
m = rise/run
b = y2/(m*x2)
print("m = " + str(m) + " and b = " + str(b))
print("the first point is: ",number)
print("and: ",number2)
print("the second point is: ",number3)
print("and: ",number4)
return # This is optional
答案 1 :(得分:2)
您要在第一次打印时从函数返回,请删除所有return
关键字。
答案 2 :(得分:2)
当您希望函数/代码流结束时,请指定return
。 return
通常是将在函数内部执行的最后一件事(不是100%真实,而是通常)。
另外,当他们实际需要返回值时,可以使用return
,如果不需要,则可以结束该函数。因此,您的代码可能应该是:
import sys
import sys
import math
import random
number = random.randint(1,100)
number2 = random.randint(1,100)
number3 = random.randint(1,100)
number4 = random.randint(1,100)
def line_equation(x2,y2,x1,y1):
rise = y2-y1
run = x2-x1
m = rise/run
b = y2/(m*x2)
print("m = " + str(m) + " and b = " + str(b))
print("the first point is: ",number)
print("and: ",number2)
print("the second point is: ",number3)
print("and: ",number4)
line_equation(number, number2, number3, number4)