简体   繁体   English

它没有打印我想要打印的所有内容

[英]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)中返回mb的值。 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.

相关问题 Python正在打印所有内容,我只希望它打印最后一个数字 - Python is printing everything, I only want it to print the last number 我想在这里打印所有内容,但%testcase引发错误 - i want to print everything here but %testcase throws error 我想在行中打印表格数据,但它在列中打印 - I want to print tabulated data in row but it is printing in column 我想在自己的值旁边打印每个 state - I want to print each state next to its own value 所以,基本上我的代码在打印我希望它打印的语句后打印 None 。 我怎样才能阻止这个 None 打印 - So, basically my code is printing None after printing the statement I want it to print. How can I stop this None from printing 尝试仅打印“状态失败”,但Python正在打印所有内容 - Trying to print out only “state failed” but Python is printing everything 我想打印列表中添加的所有元素,但它只打印最后一个元素。(Python) - I want to print all the element added in list but it is only printing the last element.(Python) 我的 python output 打印了两次,而我希望它打印一次 - My python output is printing out twice and while I want it to print out once Function 不打印,而不是打印我希望它在 Python 的装饰器中打印的内容 - Function prints none instead of printing what I want it to print in decorators in Python 我想防止打印两次 - I want to prevent printing twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM