SyntaxError with print - python

The code in the interactive shell:
>>>password = ''
>>>while password != 'secret':
password = input('Please password')
print("Right password")
SyntaxError: invalid syntax
>>>
I want to make a code where he will ask me to enter the code and i can do it ... But when i press enter I get the following message.
python3
https://imgur.com/a/7z6CaTu

Please note input() and print() in Python2.x is a little different with python3.x
Therefore, Check your python version first.
Python2.x:
password = raw_input('prompt to user')
print 'hello world'
Python3.x:
password = input('prompt to user')
print('hello world')
Then, when you define a while loop in the command line, you should leave a line empty and press Enter after while loop completed, enter the rest of your code.
As I showed in the above picture, I left a line empty then press enter, then I entered my rest of codes.
If you wanna run your code in Python v3, in the command prompt or Linux shell enter: python3
If you got error please download the latest version of python
Finally, as others say, please use an IDE like vscode and enter your entire code then run it, less trouble!!!
Download VisualStudio Code:
(https://code.visualstudio.com/download).

Python shell doesn't support the complex code. Maybe You can try this.
Python shell with while
Anyway, I recommend you to use the script.

Related

Python 3 input() function not allowing input

I haven't really ran into many issues with python but I'm working on a side project and the input() function wasn't working. I created a new file to test out a simple input() function and the same thing happened, the program doesn't take any input and it instead seems to take in the pyenv version or something? Anyone know how to fix this?
# Taking input from the user
name = input("Enter your name")
# Print input
print("Hello", name)
Pasted output from terminal:
Enter your namepyenv shell 2.7.18
Hello pyenv shell 2.7.18

'input()' Python 3 'Press Any Key to Continue'

Screen shot of the code error
I think this problem maybe because of something to do with Terminal or Visual Studio Code. I'm not sure. (Just started learning python). I am also doing this on Mac OSX
But when i try to use 'input()' as like a 'Press any key to continue i get an error saying
SyntaxError: unexpected EOF while parsing
Don't really know how this can be fixed.
I'm using :
Python 3.7.3
Visual Studio Code = IDE
Terminal = Running the code
def mainmenu():
print("Welcome to the main menu.")
print("________________________________________________________")
print("Hello and welcome to the password checker and generator.")
print("________________________________________________________")
input()
mainmenu()
So the goal here is to able to use the 'input()' function to get the user to press a key to continue.
thanks
This error usually raises when using Python 2.7, as raw_input() is then the correct function to use.
Consider checking whether you're using the correct version of Python!

My mac terminal can't run python functions

I've tried to run multiple python functions on mac terminal and they all return syntax error, for this program
def spam():
print "R"
spam()
it returned the error:
./test.py: line 1: syntax error near unexpected token `('
./test.py: line 1: `def spam():'
this is really the simplest function I could find.
Just to be clear terminal is running the rest of the program but it can't handle functions.
#!/usr/bin/python
import math
number = int(raw_input("What's your surd?"))
print type(number)
#Just to let us know what the input is
if type(number) == int:
print "Number is an integer"
else:
print "Please enter a number"
value = math.sqrt(number)
#Takes the number and square roots it
new_value = int(value)
#Turns square root of number into an integer
if type(new_value) == int:
print "Surd can be simplified"
print new_value
else:
print "Surd cannot be simplified"
print value
This program runs fine even if it is a bit buggy at the moment but the following program returns the same error as the previous function.
# define a function
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = int(input("What's your number? "))
print_factors(num)
Why terminal is returning a syntax error where there isn't one?
The problem here (at least for that first example) is that you're not using a python interpreter. The terminal is using a bash interpreter for your python code and getting very confused. Use a command like this to execute your code python spam.py . Or first enter the python command interpreter by running python, then entering your code in the command line interpreter.
What might be even easier while getting started is to get an IDE like PyCharm (https://www.jetbrains.com/pycharm/) and running through a couple of their tutorials to get the feel of it.
Your issue is that your shell doesn't know you are running a Python script. You need to make it clear that you should be using the Python interpreter. You can either do this by:
1) Call python test.py at your terminal.
2) Add #!/usr/bin/python at the top of your Python script (you may need to alter the path to the Python executable on your system). Make the script executable, and call ./test.py at your terminal.
The benefits of 2) are that you know what version of Python you will be running your script with (Python 2.x in your case?).
Method 1) will use whatever Python version is encountered first in your PATH, which may be Python 3 or Python 2, depending on whether you have installed Python 3 at some point. The code you have written will work with Python 2.7, but not Python 3.x. Of course you can always explicitly call python2.7 ./test.py.

Can't run a python program in terminal? [Mac OSX]

I'm trying to execute a python program from Terminal but it isn't working.
Something like:
x = 5
Then typing:
UserName:Documents UserName$ python simple.py
Outputs:
UserName:Documents UserName$
Without actually executing/opening the file.
But if I have a program like:
x = input('Something: ')
Then it shows up in terminal, such as:
UserName:Documents UserName$ python simple.py
Something:
Probably a silly question, but been trying to fix it for the last 1.5 hours and can't find a workable solution.
The short program
x = 5
gets run and then Python exits back to the command line. No problem there, it all works correctly. If you want to stay inside the interpreter, start your program with
python -i simple.py
When running that, you will get the usual interpreter prompt after it is finished:
>>>
and you can see it did run, because x got the expected value:
>>> x
5
>>> x*x*x*x/(x+x+x-x/x)-x/x-x/x
42
Also, from inside the interpreter, you can load-and-run your file again:
>>> execfile('simple.py')
>>> x
5
See Bojan Nikolic's Running Python Programs from the Command-line for a number of other startup options.
It looks like it is working exactly as written... but maybe not as intended... The input prompt asks you to enter a value...
maybe you want to add a print so that you get a feedback that your program does something:
x = input('Something: ')
print(x)
if you are using python 2.x:
x = raw_input('Something: ')
print x
Then, on the prompt, input a value and press enter

python cvs check out

I am writing an automation script in python and I need to cvs update from script.
In bash my commands look like this:
cvs login
CVS Password: <here i enter the password>
cvs update -P -d
I was using the sub-process module in python to do this, but it fails when it asks for the password.
Any ideas?
The pyCVS module can help you sovle the problem by using a binding.
In general, subprocesses have been, in my experience, much more trouble than just using a library that accomplishes the same thing.
Generally you can pass as arguments like this simplified code:
## program run.py
def print_args(name, passwd):
print name
print passwd
## calling program
import run
input_name = raw_input("Enter name ")
input_passwd = raw_input("Enter password ")
run.print_args(input_name, input_passwd)

Categories