Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
recursion
#1
How to write recursion on python?
I need to write something like
x sub (n)= x sub (n-1) + 10.
I am a bit lost since online is
quite impossible to find examples showing it.
Reply
#2
You just call a function from within itself. Of course, you also need a termination state to stop the recursion, which you did not specify. So here's an example with factorials:

def factorial(n):
    if n in (0, 1):
        return 1
    elif n < 0:
        raise ValueError('Factorial is not defined for negative numbers')
    else:
        return n * factorial(n - 1)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I'm sorry, but I don't find your explanation very clear.
What I would need is a recursive example similar (or the same)
to the one I wrote and translated into python code.
Reply
#4
(Apr-01-2019, 06:58 PM)mcgrim Wrote: What I would need is a recursive example similar (or the same)
to the one I wrote and translated into python code.

I can't do that. I explained why in my in my previous post. And we don't write code for people around here, we help people write their own code. So try it yourself, and if you have problems post your code here and I'll help you with it. Please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
this is what I came up with but not sure if is right.
For x sub (n)= x sub (n-1) + 10
would it perhaps be
def f(x):
    return x(i-1) + 10 
?
Reply
#6
You missed a bracket in your Python tags, I fixed it for you.

You are mixing things up a little. Your function name is f, that's what you want to call again. Your parameter is x, that's what you want to modify when you call f again.

def f(x):
    return f(x - 1) + 10
But you still have a problem here. This will keep calling f forever. Well, not quite. Eventually it will cause an error because you have recursed too deep. You need a termination state, a point where you stop calling f. In my factorial example, the termination state was 0 or 1, at which point I returned 1 rather than calling the factorial function again.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Apr-01-2019, 07:21 PM)mcgrim Wrote: this is what I came up with but not sure if is right.
For x sub (n)= x sub (n-1) + 10
would it perhaps be
def f(x):
    return x(i-1) + 10 
?
You need an if statement to make the computer decide when to stop recusing. Where does the value of i come from? Should i be another argument in your recursive function?

(Apr-01-2019, 11:42 PM)BillMcEnaney Wrote:
(Apr-01-2019, 07:21 PM)mcgrim Wrote: this is what I came up with but not sure if is right.
For x sub (n)= x sub (n-1) + 10
would it perhaps be
def f(x):
    return x(i-1) + 10 
?
You need an if statement to make the computer decide when to stop recusing. Where does the value of i come from? Should i be another argument in your recursive function?
I meant to type "recursing."
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020