0

I just started learning python. I created a simple .py file using the IDLE editor and I am trying to run it from the command prompt. However, every time it keeps giving me the "SyntaxError: Invalid Syntax" message.

This is how the .py file looks when opened with notepad:

 Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32

 Type "copyright", "credits" or "license()" for more information.

 >>> import sys

 >>> print(sys.platform)

 win32

 >>> x="Spam!"

 >>> print(x*8)

 Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!

 >>> print(2**100)

 1267650600228229401496703205376

 >>> 

And this is what I type in the cmd:

 C:\code\script1.py

Assistance would be appreciated.

5
  • 1
    That's not a py file at all. That's a copy of a shell session. What tutorial told you to put that in a file? Commented Sep 12, 2018 at 7:03
  • I ran the code in IDLE then saved it as a .py file. That isnt a script? Commented Sep 12, 2018 at 7:05
  • No not at all. You can surely see it has many things that are not Python code: those first two introduction lines, the >>> prompts, and your own output from your print statements. What would they be doing in a script? Commented Sep 12, 2018 at 7:07
  • Is there a way to get the IDLE editor to only save the code? Commented Sep 12, 2018 at 7:08
  • See my [EDIT]. Commented Sep 12, 2018 at 7:19

2 Answers 2

4

That's not a Python program, it's the log of an interactive (command prompt) session.

Instead, try entering the following in any text editor (e.g. notepad, notepad++), save it as C:\code\script2.py and then run it as you did:

import sys

print(sys.platform)

x="Spam!"
print(x*8)

print(2**100)

[EDIT] If you want to use Idle for this, click [File][New] to create a Python source code file, type in the above, save it and then run it as you did.

[EDIT2] Idle is and example of an Interactive Development Environment (IDE). Since you're new to programming: IDE's tend to obscure what's going on, although Idle isn't a severe case of this. So using a separate editor and running from the command line as you did is actually a good way to familiarize yourself with what's going on under the hood. This will pay off in many ways in the long run.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to get the IDLE editor to only save the code?
See my [EDIT] on how to use Idle for this.
0

As said before; This is not a python file...

Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

That's the shell (which you get if you just type: python) where you can type commands like:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
    print(x)

and this will instantly execute...

What you need to to is go on and create a script.py file, write your stuff in there and then execute it as: python script.py

Greeting Elias

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.