Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: new to python and programming at large. (http://www.velocityreviews.com/forums/t956380-re-new-to-python-and-programming-at-large.html)

 Michael Torrie 01-10-2013 03:01 AM

Re: new to python and programming at large.

On 01/09/2013 07:45 PM, kwakukwatiah@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.