How to program python to auto stop a long output [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I am creating a python script that I want to make it run in a loop that outputs numbers but how do I make python stop itself?
Basically my question is how do I make python use Ctrl+C or something else and stop itself?

If this is what you're saying, here is an example:
while True:
#do your number things here
if the_number == some_amount:
break
You use the break command when you reach a certain condition to break out of a while or for loop.
Hopefully this is what you wanted. If you have questions, ask away in the comments.

You can try sending the Control-C signal through os.kill().
http://docs.python.org/2/library/os.html
This is for sending the Control C singal to the process and not for ending a loop (Remolten's Solution).

Related

How to restart a .py file when it finishes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am creating a simple game that will have the character die a lot, and as a consequence will end the game. how can i make it to have something like pressing the space bar restarts the file or something along those lines? It's a very simple file, so i am just looking for a simple solution.
You could wrap your script in a
while True:
...
block, or with a bash script:
while true ; do
yourpythonscript.py

how do i loop/repeat/range(whichever you call it) print a text in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
what did i do wrong in this piece of code that it will not loop print? please send the correct answer.
repr(
"input()means you tell the computer something"
)
range(
"input()means you tell the computer something"
)
I am not too sure what do you want to achieve with this piece of code, but there are multiple ways to repeatedly print a string in python. Here are a few ways:
Using a while loop (this will run infinitely)
while True:
print("input()means you tell the computer something")
Using a for loop (if you want to print it for a definite amount of times)
times = int(input("How many times do I repeat?: "))
for i in range(times):
print("input()means you tell the computer something")
I hope that answers your question

How to make a Python script restart itself if it gets an error and stops if it doesn't? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a script that gets errors and I want to make it restart when it gets an error and stop restarting when it doesn't.
Put the script contents inside a while True loop, and only break out of the loop when you get no errors.
while True:
try:
# do stuff
# do more stuff
# yet more stuff
# if we made it to this line, no exceptions were raised, so break the outer loop
break
except Exception as ex:
print(f"Oops got an error: {ex}")
# the loop will keep going

How i can call some functions when user in raw_input in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How i can run codes ( for e.g print('hello, world') ) when user in raw_input("Enter your value")?
I just want run some codes when user writing input from raw_input function.
For e.g when you are typing your message in chat room, you can see messages of other users also.
raw_input is blocking. You would need to use multiple threads or processes to make that work, and any print-like output would clobber what you were writing on the same line. If you wanted to make a chatroom-like experience, I wouldn't recommend it. You could try curses if you wanted to stay in terminal-land.

Most efficient way to stop running a part of code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am coding a personal assitant in Python. At this moment, I am planning all the things I am going to do but I have come up with a problem that I can't solve.
I will be running a main script that will check if user says 'Hello' every 3 seconds. If he does so, then it should start running another script/function and stop the current one. After the task is performed it should start running again the main script (I will be using different scripts for each task to make it cleaner). I had thought about a while loop but I am not sure if this is the best option.
The select system call is the a very efficient way to wait until a file is ready to be read before doing something:
import select
import sys
while True:
reads, _, _ = select.select([sys.stdin], [], [], 3)
if reads:
line = reads[0].readline()
if line.strip().lower() == "hello":
# do a thing
print("hi")
Once hello is read, and your function or process is executed, your program will return to reading stdin.
Note that this works for POSIX systems but not for Windows (except for sockets).

Categories

Resources