5 Replies - 652 Views - Last Post: 08 April 2012 - 11:47 AM Rate Topic: -----

#1 whytheevanssoftware  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-April 12

if statement

Posted 07 April 2012 - 08:14 PM

Right now I have this code:

def buildConnectingString(params):
     return ";".join(["%s=%s" % (k, v) for k, v in params.items()])
if_name_=="_main_":


For some reason, the program is telling me that I have invalid syntax!(assuming that the first two limes are correct)
I just need a snipppet of code that is suitable enough for me, in order for me to continue the code.

Is This A Good Question/Topic? 0
  • +

Replies To: if statement

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: if statement

Posted 07 April 2012 - 08:45 PM

An if-statement needs to be followed by an indented block containing the code that should be executed if the given condition is true. You can't just write an if-statement without a body. If you want your if-statement to be empty (though there really is no reason that one would want that), you can use pass at the body.

Edit: Also I just noticed that there's no space after the if. The way you wrote it, it's comparing a variable names if_name_ with the string "_main_". That's a syntax error because expression COLON is not valid syntax (it would have to be IF expression COLON).

This post has been edited by sepp2k: 07 April 2012 - 08:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: if statement

Posted 08 April 2012 - 01:44 AM

Also, according to this page the correct syntax is with 2 underscores.
if __name__ =="__main__":


Was This Post Helpful? 0
  • +
  • -

#4 whytheevanssoftware  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-April 12

Re: if statement

Posted 08 April 2012 - 09:09 AM

Thanks for the help, I am still trying to learn this language for 3 hours and the reference that I had was completely inaccurate and now I have some more trouble (This will be the last time).
This will continue from my last entry!
     if _name_=="_main_":
          myParams={"server":"milgrim",\
          "database":"master",\
          "uid":"sa",\
          "pwd":"secret"\
}
print buildConnectionString(myParams)


It is telling me that myParams is not defined!
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: if statement

Posted 08 April 2012 - 11:04 AM

Indentation matters in Python, and you ignored Ionut's post. Try this:

if __name__=="__main__":
    myParams={"server":"milgrim",\
      "database":"master",\
      "uid":"sa",\
      "pwd":"secret"\
    }
    print buildConnectionString(myParams)

Was This Post Helpful? 0
  • +
  • -

#6 whytheevanssoftware  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-April 12

Re: if statement

Posted 08 April 2012 - 11:47 AM

Thanks! I was studying C++ before, and I never think about indentation. I just sit on my desk half-asleep.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1