Hi, all. I'm a very, very new programmer. My language of choice at the moment is Python, and I feel like I have a decent feel for it. I'm just now starting to learn about recursion. (By the way, if anyone could recommend a good guide on this, please let me know!) Just so you all know, this question is very elementary, and the code I'm posting is horribly, horribly wrong.
Anyway, I'm trying to write a function that will get all the friends within a specified degree. If I pass it 0 as the degree, I just want myself. If I pass it 1, I want me and all my friends. 2, I want me, my friends, and all their friends, and so on.
I've tried quite a few different ways of doing this, but none work. I try to visualize how it should work in theory, and I can't quite get that either because I'm so inexperienced in this area. Maybe a kind soul here can show me all the ways in which this code fails and then explain how to do it properly and/or recommend a good guide on the subject. Here goes:
def getFriends(self,degree,friendList):
if degree == 0:
friendList.append(self)
return friendList
else:
friendList = friendList.append(self)
for each in self.friends:
each.getFriends(degree-1,friendList)
It doesn't work, and I know I've done stupid, stupid things. Someone please slap me and point me in the correct direction!
Thanks.