Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7941877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:47:58+00:00 2026-06-03T23:47:58+00:00

I am a beginner in Python and I have written this simple code but

  • 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"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:48:00+00:00Added an answer on June 3, 2026 at 11:48 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
I'm trying to get simple code working, unfortunately I'm a python beginner. My script
Beginner programmer here. So bear with me. I have a simple python program. print
I'm a beginner in Python. My problem is pretty simple. I have a string
I'm a beginner in Python and have a file i've read in that has
beginner to python here. I have 2 nested lists that I want to merge:
I'm a beginner using python 3.2 and i have a book whos code is
I'm a beginner to Python trying to decode this javascript sequence. I'm not only
Disclaimer: I am a beginner in python but have Drupal programming experience. I have
Disclaimer: I am a beginner in python but have Drupal programming experience. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.