variable is local and global

Kyle Root kyleroot at
Mon Jul 26 04:41:33 CEST 2004

I'm writing a little program that compares two versions and tells you 
whether you are upgrading, downgrading, or it's the same version. 
Unfortunately, I haven't gotten very far, in fact I'm at a dead stop.

I need to make a variable in function available outside the function, so 
I used "global", however now the program won't run and gives me the not 
so friendly error:

bash-2.05b$ ./
   File "./", line 11
     def make_acceptable(version):
SyntaxError: name 'version' is local and global


I've looked and searched all over. Here's the code in case anyone can 
spot the problem.

import string
import sys
# This'll be replaced by an arg catching function, but for now this will 
be fine
# We don't allow versions that have anything else besides digits and periods
acceptable_chars = "1234567890."
max_count = len(pkg_version)-1
def make_acceptable(version):
	counter = 0
	while 1 == 1:
		one_char = version[(counter):(counter + 1)]
		if one_char in acceptable_chars:
			if counter == max_count:
				# Now we need to replace the periods with zeros.
				version = version.replace(".","0")
				global version
			elif counter != max_count:
				counter = counter + 1
		elif one_char not in acceptable_chars:
			print "EXITING: Versions may only contain digits and periods"

db_version = version
print db_version
pkg_version = version
print pkg_version
	Kyle ;)

More information about the Python-list mailing list