How to rerun a program in Python IDLE within the shell? - python

How do i rerun a program in IDLE shell without having to go back to the saved code and pressing F5 over and over every time I want to execute it? is there any shortcuts or hotkeys that could be pressed while in the shell?
for example i have a simple program that prints a typed message:
message = input('Enter message: ')
print(message)
once i run this simple program, i give the input, the program prints input and the program ends. Now, say i want to give a different input, i'd have to leave the shell and go back to the saved code and press F5 again. but if there was a hotkey that could be pressed while in the shell that would rerun the program, that would be awesome.

maybe add input() in the end of your code so you can see the results in console before it disappear

Related

Python3 -- Function similar to win32api.SetConsoleCtrlHandler() that works in PowerShell

Win32API commands seem not to work with PowerShell (I'm running 7, but I assume unimportant), only with the inferior IMHO Command Prompt.
I am building a program (a glorified HOSTS site blocker) that runs on the simple structure of "query command, execute command; repeat until user prompts to exit" and one of the things the user can query the program with is UNBLOCK category>site_nickname FOR minutes. This runs the program in one thread, starts a timer in another thread so the user can keep using the program as they desire. After the timer, the UNBLOCK command the program ran at start is undone so the user can get back to work. What I want to avoid is the user being able to cheat fate by saying UNBLOCK, then closing the program via the 'close' button, terminating the PowerShell instance, my Python program, and all its threads and thus ensuring the UNBLOCK command is never undone with a partner BLOCK command.
The user can exit the program by typing EXIT or CANCEL. This has the program wrap up, and then run sys.exit(). As I discovered, using the atexit library just basically does that but runs the function you want afterwards, does not intercept a close-button click. And setting the console control handler only seems to work in Command Prompt (whose support for ANSI escapes/emojis/Unicode probably too, seems to be spotty), also, the window_exit() function, or I guess the proper term is "method" --
def window_exit(signal_type):
sure = ask_input("Wait, wait! Are you sure you want to exit?") # input plus ANSI escape sequence, highlights the input prompt in blue and adds ">>" on a new line
if sure == "Yes":
evaluate("EXIT") # runs this command as if the user typed it in to be evaluated by my program
-- does not accept input properly. Instead, it displays the message and then for some reason decides I am trying to pull some "Press any key to continue ..."-type stuff, when I want the user to be able to type whatever and then have it be evaluated.
What can I do so any attempt to "cheat" and exit the program early is acknowledged and dealt with?

How do I make a code run when clicked on and take input in python?

I have a script and I want it to run when I click on it, but when I saved it as exe as other people suggested and click on it, it only flashes cmd for a split second and it does not show anything nor does it take the input it needs.
Any idea?
You can try adding a "pause" at the end of your script.
This will ensure the cmd doesn't close after your script runs and if your script hasn't run successfully then this will at least let you see the error message
Use Auto Py To Exe for converting python scripts into executable files.
Here's a description.
If you want your script to stay on screen and not open-close immediately (flash), you can make it wait for a useless input like this:
print('start of the script')
#do sth here
print('end of the script')
draft = input()

How to pause script execution in PyCharm Community?

I wrote this piece of code and tried to debug it:
from time import *
for i in range (100):
sleep(1)
print(i)
I first run this script in debug mode, and try to pause it by clicking the pause button, but the pause button doesn't work at all, it just keep printing new numbers.
Then I directly run this script (not in the debug mode), the pause did stop pycharm from printing new numbers, but the script is actually still running in the background, when I resume the script, it prints a lot of numbers all of a sudden.
So how can I correctly pause the script execution?
I installed pycharm and python in a whole new windows 7, it still behaves like this.
The stop and rerun button works perfectly, breakpoints too. But the pause button never works.
The pause ("Pause Output") button only temporarily suspends output to the terminal - it has no effect on the script execution. You may wish to use debug mode with breakpoints instead.
You can add breakpoints into your program by clicking in the space to the left of the text editor (the "Left Gutter", where line numbers appear, if you have them enabled).
See the Pycharm documentation for more information.
Update 2022
We now have a pause button in debug mode :

how do you stop commands from stacking in the console?

So basically I'm trying to make a reaction test in Python. The implementation is unimportant, but I'll leave it below:
def reaction():
print("3",end="\r")
time.sleep(1)
print("2",end="\r")
time.sleep(1)
print("1",end="\r")
time.sleep(1)
print("---------------------")
time.sleep(random.randint(1,4))
reaction_start=int(round(time.time() * 1000))
input("PRESS!")
reaction_end=int(round(time.time() * 1000))
return (reaction_end - reaction_start)
So basically, it gives a countdown before showing "PRESS!", and then it records the time it takes for the user to press enter. I plan to repeat this test several times so that the user's performance can be averaged over.
However, you could cheat this by pressing "Enter" before the "Press!" sign shows up. The "enter" command seems to stack and gets executed the next time the program asks for user input. In fact this seems to be the case in general not only for this function but for every input related thing in Python.
That is, if you make any input when running Python, those inputs gets inserted into the next possible point when the Windows console next asks for inputs. So If I run a Python script, and then while the python script is running, I press "up" and "enter", the program will immediately press up and enter after the script is done running and rerun the last command (i.e. repeat the function). How do you stop commands from stacking in this way?

my Python program shut down when i executing by double click

i write some python program.
i usually execute my program by CMD
but this time, i tried to execute program by double click
it works well until interpreter meet input code.
when i input some texts, it shut down
my input code is
for i in (input('range input => ')).split(' '):
range_list.append(int(i));
it works totally well when i execute by path(py ~.py) through the CMD
can you help me?
The interpreter is running in an endless loop. Executing your program from windows or via the command line using python will run and exit the program immediately.
At the end of your program just add
input()
This will keep it open so you can see your results.
Yeah when the program is done, it closes.
You can add something like x = input() at the end if you want to keep it open, or just run it in cmd.
Your program opens in a windowed mode when you double click it. The window will disappear / close down when the program finishes, which happens a lot fast after you have entered all the required inputs.
If you wish to see the output before the program exit, expect an input at the end of your program.

Categories