There are a few different issues with your code. The error you ask about is that you are trying to call quad_form1
, which is a variable within the quad_form
function, rather than calling the function itself. You may want to read up on functions. However, even with that issue solved, there are some additional errors that will show up.
I've tried to re-work your code to make it functional and also explain why certain things are changed:
1a. You take input as a single variable, UI, which you want to break down into three variables (a,b,c). The input will come as a string
, which you then need to split into the three different variables, and further convert these to int
or float
in order to pass them to your functions.
1b. You also want to instruct the user on how to enter the coefficients (separated by spaces or commas) and want to build in some kind of exception handling, for users that don't follow the instruction. I haven't done that for you.
It's not entirely necessary to have discriminant
as a separate function, but I've left that, since it might be useful in the context of the larger program you're working on. I've simplified it, though, so that discriminant
receives the coefficients a,b,c and returns the disc
value directly to the quad_form
function for use there. Rather than calling the discriminant
function within the quad_form1 and 2
equations, disc
is defined and then used in these equations.
The quad_form
function calculates the two possible values, in a manner similar to what you have, but now they use the variable disc
. The two solutions are returned as a list, which is then printed in the print quad_form(a,b,c)
call. You could also access individual solutions by index; for example, print quad_form(a,b,c)[0]
or print quad_form(a,b,c)[1]
Here is the edited code:
# 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)