2
from __future__ import division
import math

def main():
    the_discriminant = discrim(1,0,-4)
    print the_discriminant
    the_rest(discrim,b,a)

def discrim(a,b,c):
    discriminant = math.sqrt(math.pow(b,2)-4*a*c)
    return discriminant, b,a

def the_rest(discrim,b,a):
    x = ((-b + discriminant) / 2*a)
    y = ((-b - discriminant) / 2*a)
    print x,y

if __name__ == '__main__':
    main()

I am fairly new to Python, and I'm playing with writing functions and returning variables, I'm a little confused on how to correct the code. I am writing a quadratic solver program, but I need to use the discriminant and a,b,c values in "the rest" function. (which does the rest of the equation.) I'm kind of confused on how to return the values and use them in another function. Thanks!

4 Answers 4

7
the_rest(*the_discriminant)

or (and I prefer this method):

d, b, a = discrim(1, 0, -4)
the_rest(d, b, a)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! it seems obvious now that I think about it..sorry for the noob question! :/
2

I believe this is what you're trying to do. your discrim function returns a tuple (similar to an array). Then when you call the_rest using a * indicates that you want to send the elements of the tuple, rather than the tuple itself as one argument

from __future__ import division
import math

def main():
    the_discriminant = discrim(1,0,-4)
    print the_discriminant
    the_rest(*the_discriminant)

def discrim(a,b,c):
    discriminant = math.sqrt(math.pow(b,2)-4*a*c)
    return discriminant, b,a

def the_rest(discrim,b,a):
    x = (-b + discrim) / (2*a)
    y = (-b - discrim) / (2*a)
    return x, y

if __name__ == '__main__':
    main()

Comments

1

while jamylak's answer is correct, it can also be much more maintainable to return a simple class. Then if you ever change your function/return values/representation, the calling code:

  • is name/identifier-based; it is very flexible to change; its not order dependent, or tuple length dependent. It is also saves you typing and unnecessary duplication of ordering implied rules throughout your code.
  • if there IS a breaking change the interpreter will error on module load instead of at runtime, so you are not going to miss the error. This is because you are trying to access named members, and are not relying on some "hidden" or "implied" rule like tuple ordering that is not formalised anywhere in the code.

For a larger project this is definitely the way to go.

Comments

0

There's nothing wrong with returning tuples like in your version of discrim. But the code just doesn't make as much sense (IMO) that way.

Try it like so:

#!/usr/bin/env python

from __future__ import division
import math

def main():
    a = 1
    b = 0
    c = -4
    the_discriminant = discrim(a, b, c)
    print the_discriminant
    x, y = the_rest(the_discriminant,b,a)
    print x, y

def discrim(a,b,c):
    discriminant = math.sqrt(math.pow(b,2)-4*a*c)
    return discriminant

def the_rest(d, b,a):
    x = ((-b + d) / 2*a)
    y = ((-b - d) / 2*a)
    return x,y

if __name__ == '__main__':
    main()

3 Comments

Brian, your method looks appealing as well, but it's not working as is. I get the error "global variable discriminant not defined. (x = ((-b + discriminant) / 2*a)) What are we missing?
Sorry, it's still not working, this is what I get in the interpreter... >>> 4.0 Traceback (most recent call last): File "C:\Users\Ravi\Documents\Python Scripts\quadratic3.py", line 34, in <module> main() File "C:\Users\Ravi\Documents\Python Scripts\quadratic3.py", line 21, in main x, y = the_rest(discrim,b,a) File "C:\Users\Ravi\Documents\Python Scripts\quadratic3.py", line 29, in the_rest x = ((-b + discrim) / 2*a) TypeError: unsupported operand type(s) for +: 'int' and 'function'
Thanks Brian, looks great.. I'll keep this in mind as well! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.