new to python and programming at large.

Michael Torrie torriem at gmail.com
Wed Jan 9 22:01:09 EST 2013


On 01/09/2013 07:45 PM, kwakukwatiah at gmail.com wrote:
> thanks so much it worked.I have tried and tried.look at what I was doing.
> me = raw_input("Enter a value:")
> from math import sqrt
> def squareroot(y):
> 
>     me = squareroot(y)
>     return squareroot(y)

Congratulations!  You've just created a recursive function!  If you call
your function, squareroot() with any value at all, the program will go
into an infinite loop and never output or return anything.

While recursive functions are useful, in your case I don't think that's
what you were aiming for.  What you need to do is drop the "me =" line,
which does nothing here except put it in a loop, and modify the "return"
line to return something useful (such as a calculation, perhaps created
by calling the appropriate function in the python math library) instead
of trying to return the result of calling your own function, which will
put it into a loop.

Step through the code in your head.  Consider what happens when someone
calls your squareroot function, with, say 5 as the input.  The first
line of the function runs, and then tries to run your function again
with 5 as the input, which then tries to run your function again with 5
as the input which then tries to run your function again with 5 as the
input, etc.  Recursion is very abstract at first, but i hope you
understand why this is happening.

For more information on how to define functions in general, see
http://docs.python.org/release/2.7/tutorial/controlflow.html#defining-functions




More information about the Python-list mailing list