end="" and time.sleep() relation [duplicate] - python

This question already has answers here:
Command prompt can't write letter by letter? [duplicate]
(3 answers)
Closed 6 years ago.
import random
import time
myStr="1234567890qwertyuiopasdfghjklzxcvbnm.,*/-+>£#$½{[]}\|!'^+%&/()=?_é><`;:"
def generator():
while True:
randomLetter=random.choice(myStr)
print(randomLetter,end="")
time.sleep(0.1)
generator()
I want to slow down my while loop . If i write time.sleep it doesnt output anything .If i delete end="" part it works but i still want to write everything in one line and I also tried with "sys.stdout.write" but it didnt work again.

do sys.stdout.flush() after print

Related

How to change time in available line in python [duplicate]

This question already has answers here:
How to rewrite output in terminal
(4 answers)
Closed 2 years ago.
Let's assume there is a code which is using with anaconda prompt. While the program flowing, every second is printing to screen. However, here every second is printed the same or next line.
I want to print every second in the same place.
For example, print 30 and after 1 second later, delete 30 and print 29 to the same place.
How can I do that with python?
You can use \r to return to the start of line instead of moving the cursor to the next line (\n). Whether or not this works with your shell is an open question; e.g. IDLE occasionally has problems with this.
import time
for x in range(10):
print(x, end='\r')
time.sleep(1)
print() # to add a newline, finally
print('The end!')
After every second when you have to clear the screen you can use this :
import os
os.system('cls')

Overwriting printed statements in the IDLE in Python [duplicate]

This question already has answers here:
How to output to the same line overwriting the previous line?
(4 answers)
Closed 3 years ago.
I am looking for a method so that when something is printed continuously, it overwrite what the previous printed statement says. for example, if I am making a counter, normally the outcome in the IDLE would be: 1 2 3 4...., but however I'm looking to rewrite/overwrite what the previous printed statement says so it say "1" for a second then "2" appears but we can no longer see "1". Any suggestions? Sorry about how wordy this question is, I was having a hard time trying to write this where another person understands.
import time
arr = [1,2,3,4]
for element in arr:
print(element, end='\r')
time.sleep(1)
end='\r' in the print statement does the trick

Python counting next to one another [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 5 years ago.
I have this code but i would like the numbers to be printed next to one another but it doesn't do that it just prints down the screen instead of filling up the screen.
import random
count=2
while count>0:
print(random.randint(0,1))
replace
print(random.randint(0,1))
with
print(random.randint(0,1), end="").
See https://docs.python.org/3/tutorial/inputoutput.html

Why can't I print outside loop [duplicate]

This question already has answers here:
Python indentation mystery
(3 answers)
Closed 6 years ago.
I am a code beginner.Can anyone tell me what happend if I get a syntaxerror when putting the "print" outside the loop
Seems like you are using an interactive interpreter shell. You should hit enter after the last line of the loop before you try to print.
In future questions please write your code in the question body instead of attaching a screenshot.

Equivalent of time.sleep from python in ruby [duplicate]

This question already has answers here:
Tell Ruby Program to Wait some amount of time
(7 answers)
Closed 9 years ago.
I am trying to write program in ruby which needs to make a pause before returning text.
In python you can do this by importing time and then using time.sleep(x). How would one go about doing this in ruby.
Ex. in Python
print ("you say hello")
time.sleep(5)
print ("I say goodbye")
For your example, just use
sleep(5)

Categories