0

This is not complete, I know i must use if / else statements to return an error if a isn't equal to zero, but right now I'm just trying to get my formula to work. I am supposed to define the discriminant and quadratic formula. When I have the following code:

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

The Error is:

name quad_form1 is not defined

and it also says the same for quad_form2

Any thoughts?

Addison
  • 403
  • 8
  • 24

1 Answers1

0

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.

  1. 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.

  2. 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)
Stidgeon
  • 2,673
  • 8
  • 20
  • 28
  • thank you very much! I think I understand this a lot more, I am just starting code so I don't fully understand the concepts involved, but providing edited code to show me how the changes can be applied helped a lot. Thanks! – Addison Dec 03 '16 at 21:14
  • also I had one more question, you said that I need to split a b and c because they are separated by spaces. But doesn't input work for numbers even if they are separated by spaces? If they are separated by commas they have to be split I believe, but I thought spaces weren't counted. Can you explain how inputs work vs raw_input s? – Addison Dec 03 '16 at 21:17
  • Also is the float not necessary in the formula part? It is now saying its not callable, even if I include it in my formula or not? This is my original code btw, not from yours – Addison Dec 03 '16 at 21:21
  • input works for anything - the user can enter a wide range of characters; the whole thing is taken as a single string. you need to break that up into the separate pieces that you want (whether the breaks are commas or spaces doesn't really matter). another approach is to ask for a, b, and c as separate inputs. – Stidgeon Dec 03 '16 at 21:29
  • as to having float in the formula, the issue isn't putting in float, it's calling the function within the formula as float(function). an equation with a = float(b*2) works – Stidgeon Dec 03 '16 at 21:30
  • for 'input' vs 'raw_input', see this: http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x – Stidgeon Dec 03 '16 at 21:31