You are Here: Home >< Maths

Python recursion code help watch

1. (Original post by will'o'wisp2)
So i need to be able to write using recursion

~snip~

i need help on the above topics i'm not realyl sure where to start
I'm going to be harsh here, but I think this needs to be said.

It doesn't seem like you know even the absolute basics (about recursion or Python) that would enable you to do this properly.

This is far from the only time you've posted on here for help with a question without seeming to actually know anything about the topic.

The truth is, you may be able to work your way through to an answer like this, but without actually studying the underlying material,you're going to end up in a real mess eventually.

Are you actually attending all the relevant lectures?
2. (Original post by DFranklin)
I'm going to be harsh here, but I think this needs to be said.

It doesn't seem like you know even the absolute basics (about recursion or Python) that would enable you to do this properly.

This is far from the only time you've posted on here for help with a question without seeming to actually know anything about the topic.

The truth is, you may be able to work your way through to an answer like this, but without actually studying the underlying material,you're going to end up in a real mess eventually.

Are you actually attending all the relevant lectures?
Wat?

This is university work? 0_0
3. (Original post by FriendlyPenguin)
In that example, a is a global variable. It is defined after the function is defined, but before the function is called.
Yes that was stupid of me. I was expecting two parameters in the function so was focussing on that.
4. (Original post by Notnek)
This is looking better but I think your second return statement shouldn't be indented. There's another mistake but try this first.
https://cdn.discordapp.com/attachmen...86/unknown.png

i don't think 7 is what i'm looking for i was looking for 6=3x2
5. (Original post by will'o'wisp2)
So i need to be able to write using recursion

powers using multiplication
factorials and
the fibonacci sequence

so far i know that you layout roughly in this form of

def fib(n)
a=............
b=............
return ...... <- is the nth term

print(fib(2))

def main()

then you got your function whatnot but i need help on the above topics i'm not realyl sure where to start
An example of the start of the fibonnachi one:

def fib(n)
If n=0
Return 0
[edited]
6. (Original post by TheMindGarage)
An example of the fibonnachi one:
~snip~
This forum has a rule against posting full solutions.
7. (Original post by DFranklin)
I'm going to be harsh here, but I think this needs to be said.

It doesn't seem like you know even the absolute basics (about recursion or Python) that would enable you to do this properly.

This is far from the only time you've posted on here for help with a question without seeming to actually know anything about the topic.

The truth is, you may be able to work your way through to an answer like this, but without actually studying the underlying material,you're going to end up in a real mess eventually.

Are you actually attending all the relevant lectures?
Yup, i've been to every single computational lecture. We do some practicals and we get given sheets on how to go through it all but the recursion sheet althought it doesn't have much on recursion it keeps implying that i need to use the if statement but i don't know how to stick that in there, i mean i can do a simple multiplication

just do

def multiply(a,b)
return a*b

print(3,4) and you can get 12 but i don't know how to stick an if statement in, so far if i use something like the sheet tells me i get factorial code not multiplication in terms of addition so i don't know :/
8. (Original post by DFranklin)
This forum has a rule against posting full solutions.
Sorry - didn't know that posting a full solution to one of the parts was against the rules. Edited.
9. (Original post by Blue_Cow)
Wat?

This is university work? 0_0
Trust me. Recursions are A-level content, though this doesn't mean OP is not at uni.
10. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...86/unknown.png

i don't think 7 is what i'm looking for i was looking for 6=3x2
What is your code actually doing?

multiply(3) = 3 + multiply(2)
multiply(2) = 3 + multiply(1)
multiply(1) = 1

So multiply(3) = 3 + (3 + 1) = 7

But that's not what you want. There's a simple fix for this.
11. (Original post by TheMindGarage)
An example of the start of the fibonnachi one:

def fib(n)
If n=0
Return 0
[edited]
i know how to do fib specifically
just establish 2 base cases and carry on with life as normal
https://cdn.discordapp.com/attachmen...60/unknown.png

like that
12. (Original post by Notnek)
What is your code actually doing?

multiply(3) = 3 + multiply(2)
multiply(2) = 3 + multiply(1)
multiply(1) = 1

So multiply(3) = 3 + (3 + 1) = 7

But that's not what you want. There's a simple fix for this.
i think so, so probably so

if n==1
return 0

but that online one i was using just broke :/
13. (Original post by will'o'wisp2)
i think so, so probably so

if n==1
return 0

but that online one i was using just broke :/
What would multiply(3) give you with this change?
14. (Original post by Notnek)
What would multiply(3) give you with this change?
https://cdn.discordapp.com/attachmen...47/unknown.png
6
15. I assume that's not what you want though? I'm guessing your multiply function is meant to multiply the input by 3.

So multiply(3) should equal 9...
16. (Original post by Notnek)
I assume that's not what you want though? I'm guessing your multiply function is meant to multiply the input by 3.

So multiply(3) should equal 9...
https://cdn.discordapp.com/attachmen...88/unknown.png

i've done this but i don't think it's recursion .__.
17. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...88/unknown.png

i've done this but i don't think it's recursion .__.

So all that function is doing is returning which is why it works. It is not using recursion.

I think you should fix your last attempt because you were close. Then you can extend it to multiply(a,b).
18. (Original post by Notnek)
I assume that's not what you want though? I'm guessing your multiply function is meant to multiply the input by 3.

So multiply(3) should equal 9...
but no i want it to multiply by the previuos number so if i stick 5 in i want 5x4 to come out

then again i'm not sure how i'd do recursion with any 2 numbers
19. (Original post by will'o'wisp2)
https://cdn.discordapp.com/attachmen...88/unknown.png

i've done this but i don't think it's recursion .__.
You're right. That's not recursion.

There is not a single a line of code that makes the function call itself.

Once you've done this, I suggest trying to learn Haskell. That will force you to do/learn recursion properly.
20. (Original post by will'o'wisp2)
but no i want it to multiply by the previuos number so if i stick 5 in i want 5x4 to come out

then again i'm not sure how i'd do recursion with any 2 numbers
Do you mean that you want it to multply the previous number by 3?

I'm not sure why you want it to do this. I recommend making a function that outputs 3 x n if you input n. Then you can extend this to a multiply(a,b) function.

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: November 5, 2017
Today on TSR

University open days

Wed, 21 Nov '18
• Buckinghamshire New University
Wed, 21 Nov '18
• Heriot-Watt University
Wed, 21 Nov '18
Poll
Useful resources

Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

How to use LaTex

Writing equations the easy way

Study habits of A* students

Top tips from students who have already aced their exams

Chat with other maths applicants