[英]Its not printing everything I want it to print
This code needs to accept two points on a line and returns the values for m
and b
in the equation y = m*x + b. (m = (y1 - y2) / (x1 - x2)
此代码需要接受一条线上的两个点,并在方程
y = m*x + b. (m = (y1 - y2) / (x1 - x2)
中返回m
和b
的值。 y = m*x + b. (m = (y1 - y2) / (x1 - x2)
and b = y1 - (m*x1)
. I coded it to do that but I'm at the part where I'm listing all the information found: ("the first point is:", "and:", "the second point is:", "m =", "B =" and etc). I noticed that from the return print list its only running the first print and isn't running all 5 prints. I tried adding \n for it to write it in a new line but it didn't work. How can I make it that it lists every single return print I coded and say the 2 points that it randomly has chosen. y = m*x + b. (m = (y1 - y2) / (x1 - x2)
和b = y1 - (m*x1)
。我编写了代码来做到这一点,但我在列出所有找到的信息的部分:("第一点是:”,“和:”,“第二点是:”,“m =”,“B =”等)。我注意到从返回打印列表中它只运行第一个打印并且不是' t 运行所有 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)
"return" will literally return to the caller (therefore leaving the function). “return” 将真正返回给调用者(因此离开函数)。 That is why the remaining lines are not printed.
这就是不打印剩余行的原因。
This should do what you want.这应该做你想要的。
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
You are returning from the function at the first print, remove all the return
keywords.您在第一次打印时从 function 返回,删除所有
return
关键字。
You specify return
when you want the function/code flow to end.当您希望函数/代码流结束时,您指定
return
。 return
generally is the last thing that will be executed inside a function (not 100% truth, but generally ). return
通常是在 function 中执行的最后一件事(不是 100% 真实,但通常是)。
Also, one uses return
when they actually need to return a value, if not, you can just end the function.另外,在实际需要返回值时使用
return
,如果不需要,您可以结束function。 So your code probably should be:所以你的代码可能应该是:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.