Running program in Python [ActiveState Python on Windows]
I just started learning python and was writing programs in Python GUI Shell IDLE. The code is the following:
>>> def buildConnectionString(params):
"""Build a connection string from a dictionary of parameters.
Returns string. """
return ";".join(["%s=%s" % (k,v) for k,v in params.items()])
if __name__ == "__main__":
myParams = {"server":"mpligrim",\
"database":"master",\
"uid":"sa",\
"pwd":"secret"
}
print(buildConnectionString(myParams))
I am facing a problem while I I try to run this program. In IDLE, when I click on Run Module, a new windows opens up saying "Invalid Syntax" Here's the screenshot:
I am not able to find how to run this and would appreciate the help in proceeding further with this.
Link开发者_如何学Go: http://i.imgur.com/UzAfY.png
It looks like you've copied the header output from a shell window into your module window: You don't want your file to look like this:
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print "Hello World"
You just want this:
print "Hello World"
Delete all that other stuff.
Move the if __name__ == "__main__":
back by four spaces; your spacing in IDLE is different from that which you've copied and pasted here, the code works fine:
def buildConnectionString(params):
"""Build a connection string from a dictionary of parameters.
Returns string. """
return ";".join(["%s=%s" % (k,v) for k,v in params.items()])
if __name__ == "__main__":
myParams = {"server":"mpligrim",\
"database":"master",\
"uid":"sa",\
"pwd":"secret" }
print(buildConnectionString(myParams))
Open up a new window in idle and create this as a .py script. Then press F5
to execute, or go to run -> run module
精彩评论