473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

variable is local and global

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$ ./version.py
File "./version.py", 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.

-------------------------
#!/usr/bin/python
import string
import sys
# This'll be replaced by an arg catching function, but for now this will
be fine
db_version="2.6.0"
pkg_version="2.6.6"
# 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
break
elif counter != max_count:
counter = counter + 1
elif one_char not in acceptable_chars:
print "EXITING: Versions may only contain digits and periods"
sys.exit()

make_acceptable(db_version)
db_version = version
print db_version
make_acceptable(pkg_version)
pkg_version = version
print pkg_version
-----------------------
Thanks:
Kyle ;)
Jul 18 '05 #1
3 3883
In the function make_acceptable, 'version' is both local and global
because it's a parameter, and you have used global to make it global.
Python does not allow it because what you mean by 'version' is
ambigious; it could mean the local, or the global. Rename either the
parameter or the global and it will work. Better yet, restructure your
code so that you don't need a global; either use the return value, or
make a class. Globals are never a good idea unless the cost of writing
it again is less than the cost of writing it right the first time.

On Mon, Jul 26, 2004 at 02:41:33AM +0000, Kyle Root wrote:
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$ ./version.py
File "./version.py", 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.

-------------------------
#!/usr/bin/python
import string
import sys
# This'll be replaced by an arg catching function, but for now this will
be fine
db_version="2.6.0"
pkg_version="2.6.6"
# 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
break
elif counter != max_count:
counter = counter + 1
elif one_char not in acceptable_chars:
print "EXITING: Versions may only contain digits and
periods"
sys.exit()

make_acceptable(db_version)
db_version = version
print db_version
make_acceptable(pkg_version)
pkg_version = version
print pkg_version
-----------------------
Thanks:
Kyle ;)

Jul 18 '05 #2
thanks, I went the route of a return value.
works fine now. :)
Jul 18 '05 #3

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

Change either the name of the global variable or the name of the parameter.
They can't both be named "version".

Skip
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
9
by: Shilpa | last post by:
Hi, I just wanted to know whether we can access global variable within a local block , where both variables are having same name. For ex: int temp=5 ; { int temp=10;
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.