I am learning python while using for statement loop is not terminating itself to do so I have to use ctrl+z
as u can see nothing is happening after ......
after using ctrl+z the statement got terminated
i m window user currently working on python 2.7.10
i have already read many post for this problem but find non solution please guide me
The interpreter waits for the rest of the for loop, so just press Enter and the loop will execute.
When working in the python interpreter, a line starting with three dots signals that it is waiting for a block to continue (blocks are created by for, if, while, def, class, and other similar statements).
Even if you put everything on one line (which python only allows if the block is exactly one line) as you have done, the interpreter still waits for you to finish the block.
Anytime that enter a statement and see that, you should first make sure that you have finished what you intended to enter (as you have) and then enter just a blank line (just hit enter) on the next line to signal to the interpreter that the block is done.
Related
I have a program running on a raspberry pi zero 2 but at a certain point it just stops, no errors, no program exit, can ctrl+c perfectly fine. It just stops doing anything and I'm not sure why.
There is a lot of complicated code that runs before this and it also calls an external library so I don't want to have to scrub through everything (that's a very deep rabbit hole). I just want to know the last line it completed before it got stuck so I can un-stuck it.
Is there any way to print the last line that was executed when it gets stuck? Maybe I can print the last line that was executed when I press ctrl+C?
So, quite a simple solution really that I apparently was not aware of. Pressing ctrl+c by default will print the traceback for the last few lines. It did not work for me because I had an exception clause that dealt with any keyboard interrupt. Getting rid of that clause forced my program to crash, now I have a separate issue of finding out what
ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout.time_left()) means.
But in regard to this question I guess it has been solved, thanks to AKX for clarifying.
FIXED IT BUT WONT LET ME DELETE
As described by the title, I am trying to run a pretty long script that closes immediately. It closes immediately without showing me the output.
Here is what I've tried:
I've attempted adding input('Press ENTER to exit') and it didn't work. I also tried doing that same command but replacing exit with close and putting exit () under it, as well as exit(0)).
I've also attempted opening it with python myfile.py and nothing. I've tried almost everything I could find.
I'm on Windows, I'm running the script directly from the file, I downloaded it and attempted to run (Sorry if this isn't clear I'm new to Python). The script is long so I don't know if it'd be useful putting it here, also it's kind of private stuff.
quit_control=input("Do you know to quit? press 'Y' and enter for quit.")
while quit_control!="Y":
quit_control=input("Do you know to quit? press 'Y' and enter for quit.")
I am trying to prevent ^C from showing when a user presses CTRL+C while my script is running.
Why do I want to prevent that?
Because things like this will happen and it does not look nice:
$ python3 myscript.py
^CYour pressed CTRL+C
I know there is a similar question here, but it does not work in Python
I found an easy method!
# Returning the cursor to home and dont create a new line
print("\r", end="")
# Now we are able to print on the line where ^C would be displayed
print("Your pressed CTRL+C")
Because things like this will happen...
Just my two cents: the fact that ^C displays in the terminal is a good thing. It's confirmation that SIGINT was sent to the process, as expected. Don't try to remove it; instead, as others have suggested, start a new line if you really want to. Or just exit without printing anything additional at all, like lots of other command line applications.
This might sound like a really dulled down question but I have honestly searched everywhere for it but is there a way where once the user clicks the "exit" or "stop" button to stop there program right after you click that it will write data to a file somewhere? or would that be impossible since you closesd that program? I honestly don't know, Here's my try at it Its nothing really because I don't entirely know how to do it, but I just say this
if (onExit):
f = open('file.txt', mode='w')
f.write (data)
f.close
my onExit is just a Boolean and yeah I'm just not sure how to do it, I know how dumb that code looks btw I just didn't know how to show to you guys that I have tried looking for it other then if I showed you my history tab
Clicking an 'exit' button typically does not actually close a program immediately. Instead, the code that runs when that button is pushed also takes care of saving data.
If we are talking about a console application, which is 'closed' by ctrl-c (i.e. a KeyboardInterrupt), you can use a try-except block:
try:
raw_input()
except KeyboardInterrupt:
# save here
raise
Python does support atexit handlers, but they are most likely not the right solution to your problem.
If you're using PyDev on Eclipse, the terminate button (red square) sends a kill message to the system, which in turn will kill your program without executing further code.
As the previous answer says, you can use the atexit module, but that only works when your program ends normally.
See also: Is it possible for Eclipse to terminate gently instead of using SIGKILL?
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.