Re: new to python and programming at large.
On 01/09/2013 07:45 PM, firstname.lastname@example.org 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
|All times are GMT. The time now is 02:20 PM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.