0

I've got a script in python which looks exactly like this:

x = input("Enter your name: ")
print("Hello " + x)
input("Press<enter>")

I've saved it correctly, and when I open the .py file, the terminal opens, and then closes almost instantly. I've figured out it says SyntaxError: invalid syntax. I've checked my code and to me its correct? I'm new to Python and I'm also using Python 3.3.2, the latest version. Why is this happening?

5
  • 4
    Try running the file from a command prompt so you can see the output.
    – BrenBarn
    May 27, 2013 at 18:57
  • What do you mean by command prompt? You mean load the file from cmd? It works in the python IDLE by the way May 27, 2013 at 18:59
  • Yes, open a cmd window and do python myfile.py where myfile.py is the name of your file.
    – BrenBarn
    May 27, 2013 at 19:03
  • 1
    This seems to execute fine for me on the command line, e.g. on Ubuntu $ python3 test1.py. While you said you have Python 3.x installed, are you sure you are running it under 3?
    – Kyle
    May 27, 2013 at 19:16
  • @KyleS: In Python 3 it is a function.
    – BrenBarn
    May 27, 2013 at 19:17

4 Answers 4

0

The Python 2.x function input() can only be used with integers.

3.x can be used with both strings and integers. You are probably using Python 2.x.

For Python2.x, you must use raw_input()

To get your code to work, you must use Python3.x

0

These are the steps that I would follow to run the file:

  1. open Terminal (or Powershell if you're using Windows)
  2. go to the directory in your terminal where the python file resides that you're trying to fun
  3. run the python file using for Terminal: python filename.py or for Powershell: filename.py

This should work for you. If you're double clicking the file and trying to get it to run that way then yes, it will show a Terminal pop up and close immediately. If you're trying to open it to edit it and you're using Windows, you need to right-click the file, and select "Edit with IDLE"

0

Before it says SyntaxError, you should also see something like File ..., line ... which would give you the exact line where the error occurred.

0

Add the following lines in front of your code:

import sys

print(sys.version)
... here the rest

and launch the script the same way you did before. This will display what version of Python is really executed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.