I am a beginner in Python and I have written this simple code but there are some errors that I could not resolve :(
Please can someone help me ?
//initialization
str = None
var = None
def setvar(val):
""" Sets the global variable "var" to the value in 'val' """
if val:
print "setting variable 'var' to '$val'" // we want to print the value of 'val'
else:
print "un-setting variable 'var'"
var = val
return
if __name__ == 'main':
try:
setvar(" hello world! ") // use this function to set the 'var' variable
var.strip() // we want the leading and trailing spaces removed
print str(var)
except:
print "something went wrong"
-
3What sort of errors are you seeing? There are some indentation errors here that are going to make this code immediately invalid. If these are copy-and-paste errors, you'll want to fix the and repost the code.larsks– larsks2012年05月15日 01:36:50 +00:00Commented May 15, 2012 at 1:36
-
1// is not a comment in python # is used for commentsGreg Brown– Greg Brown2012年05月15日 01:37:40 +00:00Commented May 15, 2012 at 1:37
-
1why the use of // when you can (and in real code must) use #loki– loki2012年05月15日 01:38:04 +00:00Commented May 15, 2012 at 1:38
-
1Is your bug related with indentation? Indentation looks bit odd.Ryan Kim– Ryan Kim2012年05月15日 01:38:33 +00:00Commented May 15, 2012 at 1:38
-
1How odd, this guy had exactly the same bugs, maybe you can ask him how he fixed it? python-forum.org/pythonforum/viewtopic.php?f=3&t=29636John La Rooy– John La Rooy2012年05月15日 03:00:52 +00:00Commented May 15, 2012 at 3:00
2 Answers 2
There are a few problems with your code sample.
First, assuming that you have faithfully preserved indentation when asking your question, it's all wrong. The body of setvar should be indented from the def. Please make sure you follow the PEP guidelines and use four spaces for each indentation level.
Secondly, comment markers in Python are # rather than //.
Next, if you want to affect a global variable within your function, you need to mark it so.
Fourth, var.strip() returns the new value, it doesn't operate in-place.
Also, the value of __name__ will be __main__ rather than main, in those situations where you're running this script.
And, finally, you have made str a null variable so you can't call it like a function.
The following works fine:
# initialization
var = None
def setvar(val):
""" Sets the global variable "var" to the value in 'val' """
global var
if val:
print "setting 'var' to '$val'" # we want to print the value of 'val'
else:
print "un-setting 'var'"
var = val
return
if __name__ == '__main__':
try:
setvar(" hello world! ") # use this function to set the 'var' variable
var = var.strip() # we want the leading and trailing spaces removed
print str(var)
except:
print "something went wrong"
One other thing which may or may not be a problem. If you want to print the value of val in that first print statement, you don't use $val. Instead, you could have something like:
print "setting variable 'var' to '%s'"%(val)
And, yes, I know you don't need the parentheses around a single value, that's just my coding style, and one less thing I have to remember :-)
1 Comment
You can't use variable names inside strings in Python like you do in Perl or PHP,for example.You do like this:
print ("setting variable 'var' to '%s'" % val) # we want to print the value of 'val'
And comments starts with '#'.