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.
Related
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?
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 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
I have made a code to open an inputed python file on key command in python pygame_functions
import os, pygame_functions
if spriteclicked(Sprite1):
os.system('file.py')
Similarly how do I close an inputed python file on key command
Your question is not very clear. By 'inputed file' do you mean that the name of the file is from user input? Or that the data in the file is from user input in some way and you want to access it?
I'm going to skip past that part and try to address what I think you are asking about. The line:
os.system('file.py')
tells the OS to run the script file.py. Because you are running it with os.system() your control is limited after you do that. You run the program and do not regain control until that program exits.
If you want to be able to run the command and then stop it when the user types a key, you need to run it in a different way. You would have to run it in a subprocess or a different thread so that you still have an active thread that is not blocked. It can monitor for the user input and then have it do something to shut it down. Exactly how you would shut it down would depend to some degree on the command you ran and how you started it.
Try looking here for some guidance on replacing the os.sytem() call.
I have a python program that displays battery voltages and temperatures in an electric car. I typically run it from a command line on a Mac by simply using the up arrow to find the command to change directory, and then up arrow to find the command to run the program. We figured out how to write a script that does this automatically and saved it as an application. It works great but don't know how to exit the program. I use control C when using the command line. How do I accomplish in in a script or app? I prefer not to ask our customers to use the command line.
import sys; sys.exit()
will stop the Python program. When you call that code is up to you and depends on the details of your program (you could have it happen when a certain button is pressed, or after a certain amount of time, or when other conditions are met). Also be careful if you have to do any "clean-up" before the program ends- this also depends on the type of application.
If for some reason you want to stop it in exactly the same way as hitting Control-C, you could do
raise KeyboardInterrupt