1

Using IDLE, I created a HelloWorld.py file which says:

    print ("Hello World")

Attempting to run it from Terminal, however, results in this error:

    xxx@xxx-VirtualBox:~/Documents$ python3.2 HelloWorld.py
      File "HelloWorld.py", line 1
        Python 3.2.3 (default, Oct 19 2012, 20:10:41) 
                 ^
    SyntaxError: invalid syntax

I searched for an answer, but as you can see I am already using the ("") convention for strings and printing in the .py file, and when I try to run it in Terminal, I am typing "3.2". Though the SyntaxError specifically points out "2" as the problem, I did go ahead and try variations such as "python", "python3", and "python 3.2.3", and none of those worked as well. I am also in the correct directory.

I'm using Ubuntu on VirtualBox and using IDLE (Python 3.2).

4

2 回答 2

1

您需要删除 HelloWorld.py 文件的第一行,该行由解释器生成,不应出现在您的源代码中。

源代码文件中唯一需要的行是print("Hello World")

于 2013-06-19T00:29:21.637 回答
1

IDLE 可能会将各种 hocus-pocus 放在文件的顶部。看,如果你打开 IDLE 终端,输入一个命令,然后转到 File->Save As,IDLE 不只是保存命令。它将所有文本保存在窗口中。基本上,它与您按 Control-A 选择全部并将其粘贴到记事本中的作用相同。在 IDLE 中创建程序时,需要转到 File->New Window。这将弹出一个与外壳分开的空白窗口。这是您放置程序的地方。如果您打开保存的文件并将其修剪为唯一的文本print("Hello World!"),那么您的程序应该可以正常运行。

于 2013-06-19T16:16:34.500 回答