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

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!

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

SyntaxError with print

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.

PyCharm: Process finished with exit code 0

I am new to PyCharm and I have 'Process finished with exit code 0' instead of getting (683, 11) as a result (please see attachment), could you guys help me out please? Much appreciate it!
That is good news! It means that there is no error with your code. You have run it right through and there is nothing wrong with it. Pycharm returns 0 when it has found no errors (plus any output you give it) and returns 1 as well as an error message when it encounters errors.
Editors and scripts do not behave like the interactive terminal, when you run a function it does not automatically show the the result. You need to actually tell it to do it yourself.
Generally you just print the results.
If you use print(data.shape) it should return what you expect with the success message Process finished with exit code 0.
exit code 0 means you code run with no error.
Let's give a error code for example(clearly in the below image): in below code, the variable lst is an empty list,
but we get the 5 member in it(which not exists), so the program throws IndexError, and exit 1 which means there is error with the code.
You can also define exit code for analysis, for example:
ERROR_USERNAME, ERROR_PASSWORD, RIGHT_CODE = 683, 11, 0
right_name, right_password = 'xy', 'xy'
name, password = 'xy', 'wrong_password'
if name != right_name:
exit(ERROR_USERNAME)
if password != right_password:
exit(ERROR_PASSWORD)
exit(RIGHT_CODE)
I would recommend you to read up onexit codes.
exit 0 means no error.
exit 1 means there is some error in your code.
This is not pyCharm or python specific. This is a very common practice in most of the programming languages. Where exit 0 means the successful execution of the program and a non zero exit code indicates an error.
Almost all the program(C++/python/java..) return 0 if it runs successful.That isn't specific to pycharm or python.
In program there is no need to invoke exit function explicitly when it runs success it invoke exit(0) by default, invoke exit(not_zero_num) when runs failed.
You can also invoke exit function with different code(num) for analysis.
You can also see https://en.wikipedia.org/wiki/Exit_(system_call) for more details.
What worked for me when this happened was to go to
Run --> Edit Configurations --> Execution --> check the box Run with
Python Console (which was unchecked).
This means that the compilation was successful (no errors). PyCharm and command prompt (Windows OS), terminal (Ubuntu) don't work the same way. PyCharm is an editor and if you want to print something, you explicitly have to write the print statement:
print(whatever_you_want_to_print)
In your case,
print(data.shape)
I think there's no problem in your code and you could find your print results (and other outputs) in the tab 5: Debug rather than 4: Run.
I just ran into this, but couldn't even run a simple print('hello world') function.
Turns out Comodo's Firewall was stopping the script from printing. This is a pretty easy fix by deleting Python out of the Settings > Advanced > Script Analysis portion of Comodo.
Good Luck
I had same problem with yours. And I finally solve it
I see you are trying to run code "Kaggle - BreastCancer.py"
but your pycharm try to run "Breast.py" instead of your code.
(I think Breast.py only contains functions so pycharm can run without showing any result)
Check on tab [Run] which code you are trying to run.
Your starting the program's run from a different file than you have open there. In Run (alt+shift+F10), set the python file you would like to run or debug.

var = input("Press any key to continue") - Why does this not work?

I wrote a small text adventure in Python 3.2.2 and sent the .py file to a friend who is using a mac.
He ran the code after downloading the latest python for snow leopard and it ran alright until the line in the code: var = input("press any key to continue"). After that it just stayed at that line not producing any errors or doing much of anything, except that nothing happened when he typed anything. The characters he typed showed up at the prompt, but the program never moved forward.
I then froze the program using cx_freeze and sent it to my sister who is running windows xp (as am I) and she had the same problem. The game loaded up fine until that line.
What am I doing wrong?
Thanks!
They are probably using python 2.x, and need to press the return key. input expects a line of input, not a single character. The input function fundamentally changed between 2.x and 3.x, and the behavior you see is consistent with python 2.x.
For background on the change in behavior, see PEP 3111

Python PyDev, Prevent carriage returns from input()

EDIT-4
I've gotten my sitecustomize.py to execute, but it tosses up an error. Here's the code for it.
The error is:
Error in sitecustomize; set PYTHONVERBOSE for traceback:
RuntimeError: maximum recursion depth exceeded while calling a Python object
I'm not terribly advanced with Python yet, so I figured I'd comment out only the lines I did not think Iwould need. No encoding issues are showing up, so I just commented out lines 23-104, but that didn't help either.
EDIT-3
I also happened to have 2.5.1 installed, so I compiled another script with that.
print 'This will test carriage returns on Windows with PyDev on Eclipse Helios'
print'Type something:',
test = raw_input()
print('You entered the following ascii values:')
for c in test:
print(str(ord(c)))
This ran fine, and resulted in
This will test carriage returns on Windows with PyDev on Eclipse Helios
Type something: g
You entered the following ascii values:
103
So this is possibly a Python3 thing only? I know it's not the interpreter, because I'm able to run it in command prompt just fine. What gives?
EDIT-2
Just tested with Helios, still having the same problem. Here's my test program:
print('This will test carriage returns on Windows with PyDev on Eclipse Helios.')
print('Type something:', end='')
test = input()
print('You entered the following ascii values:')
for c in test:
print(str(ord(c)))
And here's the output when I type 'g' and press Enter:
This will test carriage returns on Windows with PyDev on Eclipse Helios.
Type something:g
You entered the following ascii values:
103
13
In the grand scheme of things, it's a small issue. I could use input().rstrip() and it works. But the workaround shouldn't even be necessary. I'm typing twice as much as I should need to in a language that I'm using because it's concise and pretty.
EDIT-1
This is Eclipse 3.5. Unfortunately that's the latest version that's been approved for use at work. I'm going to try 3.6 at home to see if that's any different, but I wouldn't be able to use it anyway.
(original question)
I've been learning some basic Python, and decided to go with PyDev since it supported Python 3 as well as having all the nice code snippet and auto complete features.
However, I'm running into that darned carriage return issue on Windows.
My searches always lead me back to this mailing list:
http://www.mail-archive.com/python-list#python.org/msg269758.html
So I have grabbed the sitecustomize.py file, tried to include it in the Python path for my configured interpreter, as well as my project, but to no avail.
Has anybody else managed to work through this? Or maybe knows how to get the new sitecustomize.py to actually execute so it can override input() and raw_input()?
I know I could always make a short module with my own replacement input() function, but I'd really like to fix the problem at its root. Aptana acknowledges the issue ( http://pydev.org/faq.html#why_raw_input_input_does_not_work_correctly ) but offers no solution. Thanks in advance for your help.
Figured out a hack to make it work locally to my Python installation. In \Lib\site-packages\ make a script called "sitecustomize.py", and put this code in it:
original_input = builtins.input
def input(prompt=''):
return original_input(prompt).rstrip('\r')
input.__doc__ = original_input.__doc__
builtins.input = input
I don't know anything about the side effects of this, or what sort of error checking I should be doing, but it works if you're using PyDev on Windows to write scripts with Python3.
Found out some more things about sitecustomize.py and how it relates to site.py.
I don't know how to add my own sitecustomize.py to PYTHONPATH for execution only in a PyDev project, so I just stuck it in ${Python31dir}\Libs\site-packages. The module runs now, but generates errors.

Categories