Pausing a while loop [duplicate] - python

This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 8 years ago.
I have a query for a small program that I am running. I was wondering if there was any way to pause a while loop in python for an amount of time? Say for example if I wanted it to print something out, then pause for say (1) second, then print it out again?
There is not really a point to this program I am more doing it for something to do while bored.
I have checked other questions and none seemed to really answer what I was asking.

import time
while ...
print your line
time.sleep(1) # sleep for one second
time sleep method

Yes. There is a function called sleep that does exactly what you want:
while true:
print "Hello!\n"
time.sleep(1)

Related

How would I be able to have certain lines of code be executed randomly? [duplicate]

This question already has answers here:
Percentage chance to make action
(4 answers)
Closed 10 months ago.
I feel this should be an obvious answer, however, I am having issues coding a personal project of mine. How would I be able to have certain lines of code be executed randomly?
Not from the project itself but the principles are still, say for example I would want the following code to be executed every 1/1000 times or so.
print("Lucky!")
How would I exactly be able to do that?
Set a trace function that will have a 1/1000 chance to print:
import random, sys
def trace(frame, event, arg):
if random.random() < 1/1000:
print("Lucky!")
return trace
sys.settrace(trace)
You're welcome to test it with a 1/2 chance.
Generate a random integer in the range [0,1000). Pick any single value in the range, and check for equality to see if you should print.
import random
def lucky():
if random.randrange(1000) == 42:
print("Lucky!")

python delay a program for n seconds [duplicate]

This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 3 years ago.
is there a function in python that delays a program from running for n number of seconds?
I am trying to achieve something like this:
print("Hello")
delay(5) #delays the program by 5 seconds
print("World!")
Any help will be greatly appreciated, Thank you!
Sure!
import time
print("Hello")
time.sleep(5) #delays the program by 5 seconds
print("World!")

Making a delay between typed characters [duplicate]

This question already has answers here:
How can I flush the output of the print function?
(13 answers)
Closed 4 years ago.
First, look at this:
import time
wait = time.sleep
def put(char):
print(char, end='')
def text(*pieces):
for p in pieces:
for c in p:
put(p)
wait(0.25)
I wanted to make a function that prints characters one by one with a delay of 250ms. But the problem is, it doesn't actually print characters one by one, instead, nothing happens until the "for" loop ends, then the given pieces of text is printed directly at once. Can someone tell me another way to do that, as in Undertale, typing/printing/putting characters one by one with a delay? Thanks.
You need to add flush=True to your print statement, otherwise the system will wait till the for loop is done. Note: this is only done if you're printing with end=''.
wait = time.sleep
def put(char):
print(char, end='', flush=True)
def text(pieces):
print(pieces)
for p in pieces:
put(p)
wait(0.25)
text('arsasrtrasars')

print without new line only appear after I write something else [duplicate]

This question already has answers here:
Printing on the same line with time.sleep()
(2 answers)
Closed 8 years ago.
I'm using Python 2.7.3 and I running the code below
def t2():
print "Waiting...",
time.sleep(3)
print "done."
time.sleep(1)
print "test"
time.sleep(2)
print "testing"
When I run this code, the string "Waiting... done." appear at same time. It's like the sleep(2) is before the first print.
If I don't use comma to remove new line (Like "test" and "testing" examples), sleep function works ok but I get "Waiting..." and "done." on different lines.
I already tried:
for i in range(0, 5): time.sleep(1)
and
subprocess.check_output(["sleep", "5"])
What can I do?
Thank you.
Depending what you are working with data doesn't necessarily get written right away. In particular,
display output often waits until it receives a newline before printing anything.
flush() makes sure it all gets written right now.
Background reading that helps explain better than I can:
Usage of sys.stdout.flush() method

Python 2.x ; function to wait a few second for continue [duplicate]

This question already has answers here:
How do I make a time delay? [duplicate]
(13 answers)
Closed 8 years ago.
I've just made a program, but I want to make a function in one place just to wait a few seconds to continue working. Could you tell me how does that function look?
for example
print 'I know'
(HERE wait few seconds to continue working)
print 'I am so layman with my trivial question'
import time
print 'I know'
time.sleep(1) #number of seconds to wait
print 'I am so layman with my trivial question'
Documentation

Categories