-2

I am trying to follow the tutorial found here: http://www.diveintopython.net/getting_to_know_python/index.html

Here's what I am doing ...

$ cat odbchelper.py
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":"mpilgrim", \
            "database":"master", \
            "uid":"sa", \
            "pwd":"secret" \
            }
    print buildConnectionString(myParams)

... and when I run the above script I get this output ...

$ python3 odbchelper.py
  File "odbchelper.py", line 13
  print buildConnectionString(myParams)
                          ^
SyntaxError: invalid syntax

... I am not sure what is wrong here. Is it python3 thing?

Thanks

4

1 回答 1

3

Dive Into Python is written for Python 2, not Python 3. If you want to use it for Python 3, you'll have to make modifications to the code.

In this case, print should be used as a function:

print(buildConnectionString(myParams))
于 2013-08-17T06:45:34.903 回答