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?