简体   繁体   中英

SyntaxError when entering python3.2 to run print from Terminal

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).

You need to remove the first line of your HelloWorld.py file, that line is generated by the interpreter, and should not appear in your source code.

The only line you need in your source code file is print("Hello World")

IDLE probably put all sorts of hocus-pocus at the top of your file. See, if you open the IDLE terminal, enter a command, and go to File->Save As, IDLE doesn't just save the command. It saves all the text in the window. Basically, it does the same thing as you pressing Control-A to select all and paste it in Notepad. When you create a program in IDLE, you need to go to File->New Window. This will pop up a blank window seperate from the shell. This is where you put your program. If you open up the file you saved and trim it so the only text is print("Hello World!") , then your program should run fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM