We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,140 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Variable not defined?

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?

3
Contributors
6
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
hovestar
Newbie Poster
12 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
Moderator
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

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

hovestar
Newbie Poster
12 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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)
hovestar
Newbie Poster
12 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
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
Moderator
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
Moderator
6,308 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0753 seconds using 2.7MB