The Student Room Group

Python recursion code help

Scroll to see replies

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?
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
Reply 22
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.
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/attachments/322819040059850757/376750266994917386/unknown.png

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

multiplication using addition
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]
(edited 6 years ago)
Original post by TheMindGarage
An example of the fibonnachi one:
~snip~
This forum has a rule against posting full solutions.
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 :/
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.
Reply 28
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.
(edited 6 years ago)
Reply 29
Original post by will'o'wisp2
https://cdn.discordapp.com/attachments/322819040059850757/376750266994917386/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.
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/attachments/322819040059850757/376757131421941760/unknown.png

like that
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 :/
Reply 32
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?
Reply 34

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...
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/attachments/322819040059850757/376759130771161088/unknown.png

i've done this but i don't think it's recursion .__.
Reply 36
Original post by will'o'wisp2

a+a(b1)=a(1+b1)=aba+a(b-1) = a(1 + b - 1) = ab

So all that function is doing is returning a×ba\times b 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).
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
Original post by will'o'wisp2


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.
(edited 6 years ago)
Reply 39
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.

Quick Reply

Latest