我正在制作一个二次型计算器,接受 a、b 和 c,其中 a 不等于 0

I am making a Quadratic Form Calculator, accepting a, b, and c where a isn't equal to 0

这还不完整,我知道我必须使用 if / else 语句来 return 如果 a 不等于零则出错,但现在我只是想让我的公式起作用.我应该定义判别式和二次公式。当我有以下代码时:

def descriminant(a, b, c): #Setting input perameters for descriminant

    disc = (b**2-4*a*c) #Defining what descriminant does with disc
    return disc #returns and allows to be used again instead of print which doesnt allow you to use it again

def quad_form(a, b, c): #Defining quad form w/ input a, b, c

    quad_form1=(-1*b + float((descriminant**.5 (a, b, c))/2*a)) #Defining + forumula for quad form
    quad_form2=(-1*b - float((descriminant**.5 (a, b, c))/2*a)) #Defining - forumula for quad form

    return quad_form1
    return quad_form2

UI=input("Enter the coefficients of a quadratic where A is not equal to zero: ")
QF1=quad_form1(UI)
QF2=quad_form2(UI)
print QF1, QF2

错误是

名称quad_form1未定义

quad_form2

也一样

有什么想法吗?

您的代码存在一些不同的问题。您询问的错误是您试图调用 quad_form1,这是 quad_form 函数中的一个变量,而不是调用函数本身。您可能想阅读有关功能的信息。但是,即使该问题已解决,还会出现一些其他错误。

我已尝试重新编写您的代码以使其正常运行,并解释了某些内容发生更改的原因:

1a。您将输入作为单个变量 UI,您希望将其分解为三个变量 (a、b、c)。输入将以 string 的形式出现,然后您需要将其拆分为三个不同的变量,并将它们进一步转换为 intfloat 以便将它们传递给您的函数。

1b。您还想指导用户如何输入系数(以空格或逗号分隔),并希望为不遵守说明的用户构建某种异常处理。我没有为你做过。

  1. 并非完全有必要将 discriminant 作为一个单独的函数,但我保留了它,因为它可能在您正在处理的较大程序的上下文中有用.不过,我已经简化了它,因此 discriminant 接收系数 a、b、c 和 returns disc 值直接到 quad_form 函数以供在那里使用。不是在 quad_form1 和 2 方程中调用 discriminant 函数,而是定义 disc 然后在这些方程中使用。

  2. quad_form 函数计算两个可能的值,其方式与您所拥有的类似,但现在它们使用变量 disc。这两个解决方案作为列表返回,然后在 print quad_form(a,b,c) 调用中打印。您还可以通过索引访问单个解决方案;例如,print quad_form(a,b,c)[0]print quad_form(a,b,c)[1]

这是编辑后的代码:

# 2. calls to the discriminant function now return the discriminant, 
# where the parameters a,b,c are from the parsed user input
def discriminant(a, b, c):
    return (b**2-4*a*c) 

# 3. disc is the call to the discriminant function, which is then used 
# in your quad_form equations
def quad_form(a, b, c): 
    disc = float(discriminant(a,b,c))
    quad_form1 = ((-1*b) + (disc**.5))/(2*a)
    quad_form2 = ((-1*b) - (disc**.5))/(2*a) 
# the two solutions are returned as a list; not the only way to handle this
# but hopefully is clear in how it works
    return [quad_form1, quad_form2]

# 1. user input must be converted to three different inputs (a,b,c)
UI = input("Enter the coefficients of a quadratic, separated by a space, where A is not equal to zero: ") 
# there are multiple ways to do this; this is not the shortest, but hopefully is clear 
# the input is split on the separating spaces, which creates a list
# each list item is then assigned to a variable, and also converted to int
# could convert to float instead, if desired
a = int(UI.split(" ")[0])
b = int(UI.split(" ")[1])
c = int(UI.split(" ")[2])

# call to the quad_form function calls to discriminant function, passing a,b,c
# disc returned to quad_form, and quad_form returns list of two solutions
# output is printed list of solutions; can be altered as needed
print quad_form(a,b,c)