Re: new to python and programming at large.

Discussion in 'Python' started by Michael Torrie, Jan 10, 2013.

  1. On 01/09/2013 07:45 PM, 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
    Michael Torrie, Jan 10, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    135
  2. Ulrich Eckhardt

    new to python and programming at large

    Ulrich Eckhardt, Jan 9, 2013, in forum: Python
    Replies:
    1
    Views:
    140
  3. Chris Angelico

    Re: new to python and programming at large

    Chris Angelico, Jan 9, 2013, in forum: Python
    Replies:
    1
    Views:
    149
    Chris Angelico
    Jan 9, 2013
  4. John Gordon

    new to python and programming at large.

    John Gordon, Jan 9, 2013, in forum: Python
    Replies:
    1
    Views:
    165
  5. Dave Angel
    Replies:
    0
    Views:
    107
    Dave Angel
    Jan 9, 2013
Loading...

Share This Page