I'm trying to devise a program that attempts to solve the Collatz Conjecture, Wikipedia, but when I run it I get this error:

What value would you like to find if the collatz works for below?6
Traceback (most recent call last):
  File "/private/var/folders/fL/fL1lGxccGCypPBSIUXNGZGpkwIg/-Tmp-/Cleanup At Startup/Playground-345866896.786.py", line 27, in <module>
    collatzcomplete()
  File "/private/var/folders/fL/fL1lGxccGCypPBSIUXNGZGpkwIg/-Tmp-/Cleanup At Startup/Playground-345866896.786.py", line 9, in collatzcomplete
    counter()
  File "/private/var/folders/fL/fL1lGxccGCypPBSIUXNGZGpkwIg/-Tmp-/Cleanup At Startup/Playground-345866896.786.py", line 13, in counter
    if value > 1:
UnboundLocalError: local variable 'value' referenced before assignment
logout

[Process completed]

Here is my code:

pre = 1
value = 1
count = 0

def collatzcomplete():
	finder = int(raw_input("What value would you like to find if the collatz works for below?"))
	pre = 1
	count = 0
	value = pre
	while finder > pre:
		counter()
		pre = pre + 1

def counter():
	if value > 1:
		if value % 2 == 0:
			value = value / 2
			count = count + 1
			counter()
		else:
			value = 3 * value + 1
			count = count + 1
			counter()
	elif value < 1:
		print "This value defies the collatz conjecture:", pre
	else:
		print pre, "has", count, "to complete the collataz"
		
collatzcomplete()

Any Ideas?

Recommended Answers

All 6 Replies

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.

I had to define it globally thanks for your help though.

And here is the code;

pre = 1
value = 1
count = 0

def collatzcomplete(p, v, c):
   pre = p
   value = v
   count = c
   
   
   finder = int(raw_input("What value would you like to find if the collatz works for below? "))
   while finder > pre:
      value = pre
      counter(pre, value, count)
      count = 0
      pre = pre + 1

def counter(pre, value, count):

   if value > 1:
      if value % 2 == 0:
         value /= 2
         count += 1
         counter(pre, value, count)
      else:
         value = 3 * value + 1
         count += 1
         counter(pre, value, count)
   else:
      print pre, "has", count, "to complete the collataz"
     

collatzcomplete(pre, value, count)

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()

And if you think little where start parameter is used, it is not, and can be removed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.