counter has not local parameter or variable named value. It is local because it is used at left side of assignement in function. You probably should have the variable as parameter of recusive function counter. You will hit the limit of recursion pretty soon so you better use regular loop.
pyTony
pyMod
6,308 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26
woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 779
Skill Endorsements: 9
Question Answered as of 1 Year Ago by
woooee
and
pyTony What global variable are you talking about?
Your code looks like giving correct result, but I must say that it is very unclear, my version for recursive version with more clear for loop and more understandable variable names (while loop would be simpler and more reliable), moving print out of calculating function...:
def collatz():
for current in range(1,int(raw_input("What now would you like to find if the collatz works for below? "))+1):
print "%i has %i to complete the collatz" % (current, step_counter(current, current, 0))
def step_counter(start, now, step_count):
assert now >= 1
return step_count if now == 1 else step_counter(start, 3 * now + 1 if now & 1 else now // 2 , step_count + 1)
collatz()
pyTony
pyMod
6,308 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26
And if you think little where start parameter is used, it is not, and can be removed.
pyTony
pyMod
6,308 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26