I am trying to run a program on a jupyter notebook that accepts user input, and I cannot figure out how to get it to read standard input. For example, if I run the code with shift-enter:
a = input()
print(a)
the cell indicates it is running, but does not accept input from me. How do I get it to accept input?
Use the raw_input() (for Python 2) or input() (for Python 3) methods.
Example code:
a = raw_input()
print(a)
Example notebook:
I came across the same problem, using the input in jupyternotebook, it blocks the execution and it does not work until restarting the program, so I added a print () after each input and my program is working.
Probably you hit Shift-Enter a second time without completing the first input with Enter, so the kernel was always waiting until the first command completed, before executing it again. If you use in the menu
"Kernel",
"Interrupt",
all active commands are stopped (including the second execution of the box) and the problem should be solved without restarting the computer (or the browser / the kernel).
You are doing it right, you ony have to restart the kernel (over the Run button)
use raw_input instead of input if you are using python 2 version. if u still getting same problem then,
click on kernel then "restart and run all"
and try to run the code again.
this will fix it.
Related
This opens whenever I start writing code in a kernel..stack.imgur.com/GQPYA.png
Expected: print("hellooo")
i am unable to type in the kernel, there's no error message, since there's no code.
I tried doing this multiple times, by closing and starting it again, but same thing happens over and over again.
Nothing to worry you have just opened command palette, search for confirm restart kernel and click it , or simply press the esc you should be ready to go.
I'm trying to debug a circuitpython program (that's running on a microcontroller) and I would like to know if there's a simple way to get the program to drop into the REPL upon a crash/termination while preserving the variables and functions defined in the script.
If this was a regular python program I would simply run it with the "interactive" option of the interpreter set : python -i my_code.py and then have access the variables and function defined in my code for easy debugging.
Instead what I get right now is: after a crash I get prompted to press a key to enter the REPL but the memory is cleared from any trace of my previously running code.
A somewhat cumbersome way to achive an equivalent behaviour, that only works if the code terminates and doesn't crash, is to proceed as follows :
Upload the code
Code will start running automatically
Interupt the code with a keyboard interupt
Press a key to get to the REPL
Import all from the code from the REPL by typing in:
from code import *
Wait for the code to terminate
Finally debug
Rince and repeat for each bug you find...
I wrote a program in my editor and clicked run(F5). Afterwards, in the python shell, I keyed in the desired inputs. What should i do next to make it start running? Attached picture is python editor and python shell.
Thanks!
Use CTRL-D to stop entering new sys.stdin.read() input. Also don't use this.
https://teamtreehouse.com/community/whats-the-difference-between-sysstdinread-and-input
I use Alt+Shift+E to send a selection of code from the editor to the (IPython) console. But, I am not able to tell when the code has completed executing, since the next prompt appears even though the previous code chunk might not have completed executing. So:
Either I have to try and send another selection to the console, and the editor warns me that the previous command has not completed running, or,
I have to try and enter something at the console, and if the results of the requested computation are not returned (print 2 + 2, say), then I know that the previous command has not completed execution.
Here is a screenshot to show what I mean:
Am I missing some feature that tells me that a selection sent to the console has not completed executing?
As an example, R will not show the next prompt until one chunk has finished execution.
It is not a pycharm feature, but your print statements will execute after the previous code is finished running, in a way, letting you know everything is finished (my programming instructor always would put print "Ready" at the end of everything for this reason).
>>> import time
>>> time.sleep(15)
>>> print "hello"
# 15 seconds later
"hello"
This is now resolved in the latest builds of PyCharm. Thanks to JetBrains for fixing this.
I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).