Hi I am a beginner to Python. I am writing the following sample code for input but when I press Enter after entering value for input command, the program doesn't go forward after printing the first line. I've done it in both Atom and Sublime Text. A basic print statement works perfect.
print ("hello")
x = input("Enter value")
print(x)
Sublime does not support user input unless you install a third-party plugin. And Atom is no different.
If you like to input values interactively, I would suggest running your script from the app Python IDLE then go to file and open your Python script and run it.
Or, alternatively, open a CMD (if you are PC user) or Terminal (in case of MacOS) and execute your script directly from the command line interface, like below:
python yourscript.py
may be the problem is with the code editor. Try running the file using cmd if you are on windows or terminal on mac/linux.
python file_name.py
Related
I have downloaded Atom.io for window and done setup successfully, then I download the package for running codes. but the Python result always come with an error like the picture.
Can you help me on this issue? Thank you
Try typing in:
python myprogram.py
However, you will still receive an invalid syntax error because:
print(*hi*)
should be:
print('hi')
Also, if you want to enter the python terminal, type:
python
Then you can type these commands (i.e. print("hi")) directly into the terminal instead of creating a .py file and running it.
One final point, I use the following instructions to add the python to PATH: how to add python to path in windows.
You can check this Stack Overflow thread for more details.
However, if you just want to play around with Python in Windows:
Download Python and install it.
Go to Start > type "IDLE" and click "IDLE (Python)".
Start coding! For example, type print("Hello, world!") and press the Enter key.
My first contact with Python was IDLE and I liked it a lot. Have fun!
It looks like you're running this from powershell. You can tell it's powershell because it says "Windows Powershell" on the first line of the output. Here's what I would do if I were you:
To start the python shell type python and press enter.
1a. Enter what you want to run in python, print('test') for example
If you want to get out of the python shell, enter ctrl-z and press enter to return to powershell.
To run python scripts use the command python {script}.py
Installing python on path
If you tried the above steps and got an error to the effect of "I don't recognize python" you should ensure that python is correctly installed in your PATH.
You can follow these instructions to install python correctly.
I have installed Python and written a program in Notepad++.
Now when I try to type the Python file name in the Run window, all that I see is a black window opening for a second and then closing.
I cant run the file at all, how can run this file?
Also I want to tell that I also tried to be in the same directory as a particular Python file but no success.
I assume you are running the script with command python file_name.py.
You can prevent closing of the cmd by getting a character from user.
use raw_input() function to get a character (which probably could be an enter).
It sounds like you are entering your script name directly into the Windows Run prompt (possibly Windows XP?). This will launch Python in a black command prompt window and run your script. As soon as the script finishes, the command prompt window will automatically close.
You have a number of alternatives:
First manually start a command prompt by just typing cmd in the Run window. From here you can change to the directory you want and run your Python script.
Create a Windows shortcut on the desktop. Right click on the desktop and select New > Shortcut. Here you can enter your script name as python -i script.py, and a name for the shortcut. After finishing, right click on your new shortcut on the desktop and select Properties, you can now specify the folder you want to run the script from. When the script completes, the Python shell will remain open until you exit it.
As you are using Notepad++, you could consider installing the Notepad++ NppExec plugin which would let you run your script inside Notepad++. The output would then be displayed in a console output window inside Notepad++.
As mentioned, you can add something to your script to stop it completing (and automatically closing the window), adding the line raw_input() to the last line in your script will cause the Window to stay open until Enter is pressed.
Try to open in Command Prompt instead of run window. The syntax is:
py filename.py
If it doesn't work, try to reconfigure Python. Did you set environment variables? If not, this could help you
I've written a script that works great at a command prompt, but it only displays the last line if I double click on the script.py icon on my desktop.
The function in the script runs perfectly but once it finds a match it's supposed to display the output on the screen. At the end, I have an os.pause statement and THAT displays when the script is done, but nothing else displays on the screen.
I AM executing it using pythonw.exe. What else should I check?
Thank you.
pythonw supresses the console window that is created when a python script is executed. Its intended for programs that open their own GUI. Without pythonw, a python gui app would have its regular windows plus an extra console floating around. I'm not sure what pythonw does to your stdout, but os.isatty() returns False and I assume stdout/err are just dumped into oblivion. If you run a DOS command like os.system("pause"), Windows creates a new console window for that command only. That's what you see on the screen.
If you want to see your script output, you should either run with python.exe and add an input prompt at the end of the script as suggested by #GaryWalker, or use a gui toolkit like tcl/tk, Qt or wxPython.
I've used a script like before and it seemed ok to me
print("Hello world")
input("Press return to exit")
I've recently started learning python and got tired of running it on the command line (terminal mac os x). I wanted an environment that I could code nicely in and run that code only when pieces of it were done, not line-by-line as in the shell. I decided to use Xcode as the interface is clean and simple, and followed this tutorial to set up Xcode so that I could run python scripts.
My problem is that when I use raw_input() and then click run, I can't type a value to pass to the variable it's stored in. Take this simple line for example:
word = raw_input("Enter a word: ")
Later on in the program, word is printed. When I click run on Xcode, the prompt shows up as expected in the console:
Enter a word:
However, I can not type anything into it, the cursor is blinking so I know it is responding but when I type a value, nothing happens. I'm not sure what is wrong here, hopefully one of you can help me out.
If you really want to use an IDE for Python development, go for Aptana Studio 3 .
Once you are done with Pydev setup which is very easy Refer this for Pydev Setup.
Before running a script which expects some command line inputs you can click on the small arrow button adjacent to run button, it will give you a drop down menu with different options. Click on run configurations and then click on a tab named Arguments and enter your parameters there in the field names Program Arguments. Click on apply and you are good to go.
Next time you need to run the code just click on run and it will automatically fetch arguments that you have specified in the option above.
Hope this help.
You just need a text editor to store you Python script and run it in Terminal whenever you like. As an example, I use emacs. You may use vi, Sublime Text 2, TextWrangler or many other alternatives.
$ emacs a.py
word = raw_input("Enter a word: ")
print "Your word is: %s" % word
After the a.py file is saved, simply cd to the directory and run the script in Terminal.
$ python a.py
Enter a word: Hello
Your word is: Hello
Or import your script as a module under interactive mode.
$ python
>>> import a
Enter a word: Hello
Your word is: Hello
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?
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
These are the steps that I would follow to run the file:
open Terminal (or Powershell if you're using Windows)
go to the directory in your terminal where the python file resides that you're trying to fun
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"
Before it says SyntaxError, you should also see something like File ..., line ...
which would give you the exact line where the error occurred.
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.