0

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"
Remy Lebeau
610k36 gold badges516 silver badges875 bronze badges
asked May 15, 2012 at 1:34
6
  • 3
    What 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. Commented May 15, 2012 at 1:36
  • 1
    // is not a comment in python # is used for comments Commented May 15, 2012 at 1:37
  • 1
    why the use of // when you can (and in real code must) use # Commented May 15, 2012 at 1:38
  • 1
    Is your bug related with indentation? Indentation looks bit odd. Commented May 15, 2012 at 1:38
  • 1
    How 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=29636 Commented May 15, 2012 at 3:00

2 Answers 2

4

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 :-)

answered May 15, 2012 at 1:38
Sign up to request clarification or add additional context in comments.

1 Comment

i've done it and it works better than fine !! :D thank you man :)
1

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 '#'.

answered May 15, 2012 at 1:44

2 Comments

hi @Ceramic Pot, i did what you said and i still have this error: Encountered "print" at line 13, column 5. Was expecting: "<INDENT>"
The indentation in your code is wrong.See paxdiablo's answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.