Factoring Polynomials

James Mills prologic at shortcircuit.net.au
Fri Dec 19 04:14:12 CET 2008


On Fri, Dec 19, 2008 at 12:48 PM, Collin D <collin.day.0 at gmail.com> wrote:
> UPDATE:
>
> #import
> from math import sqrt
>
> # collect data
> a = float(raw_input('Type a value: '))
> b = float(raw_input('Type b value: '))
> c = float(raw_input('Type c value: '))
>
> # create solver
> def solver(a,b,c):
>    disc = b**2 - 4*a*c
>    if disc < 0:
>        return 'No real solution.'
>    else:
>        sol1 = (-b + (sqrt(disc))) / (2*a)
>        sol2 = (-b - (sqrt(disc))) / (2*a)
>        return (sol1, sol2)

You do not need the else here.
return will always return control
to the calee.

Try this:

class Unsolveable(Exception): pass

# create solver
def solver(a, b, c):
   disc = b**2 - 4*a*c
   if disc < 0:
       raise Unsolveable()

   return -b + (sqrt(disc) / (2*a)), -b - (sqrt(disc) / (2*a))



More information about the Python-list mailing list