Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: new to python and programming at large.

Reply
Thread Tools

Re: new to python and programming at large.

 
 
Michael Torrie
Guest
Posts: n/a
 
      01-10-2013
On 01/09/2013 07:45 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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/t...ning-functions

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new to python and programming at large Ulrich Eckhardt Python 1 01-09-2013 09:05 PM
new to python and programming at large kwakukwatiah@gmail.com Python 0 01-09-2013 09:03 PM
Re: new to python and programming at large Chris Angelico Python 1 01-09-2013 03:43 PM
New to programming AND new to Ruby Cassandra K. Ruby 10 02-04-2011 08:07 PM
Can Your Programming Language Do This? Joel on functional programming and briefly on anonymous functions! Casey Hawthorne Python 4 08-04-2006 05:23 AM



Advertisments