user7437114
user7437114

Reputation:

How to print a string one word at a time, on one line with all of the string visible. - Python

I want to make the word: 'error' repeat itself numerous times. This is an easy task in itself, but I do not know how to print them one at a time, at whatever speed I choose. I understand that my question has been partially covered, but I do not wish for the previous word of the string to dissapear.

 print('Error! ' *20)

The output that I want is Error! Error! (each released individually instead of being released all at once.)

How can I make it so that python does not release all of the string at the same time? It would also be good to know whether I can make it print in columns, but still along the widths.

Upvotes: 0

Views: 134

Answers (3)

cs95
cs95

Reputation: 402363

import time
for _ in range(20):
    print('Error!', end=' ', flush=True) # flush courtesy inspectorG4dget to disable buffering
    time.sleep(0.2)

This'll print 'Error!' every 200 milliseconds on the same line (...20 times). Change that for to a while for better control over the stopping condition.

Upvotes: 1

Easton Bornemeier
Easton Bornemeier

Reputation: 1936

import time
for i in range(20):
    print ("Error!")
    time.sleep(60) #pause for one minute

You can use a for loop to choose the number of times to print, and sleep() from the time library to do the pausing.

Upvotes: 0

inspectorG4dget
inspectorG4dget

Reputation: 113915

This will print Error! 20 times (on 20 lines/rows), over a duration of 20 seconds

import time

for _ in range(20):
    print("Error!")
    time.sleep(1)  # change 1 to whatever number you want, to control the amount of time before "Error!" is printed again

This will print "Error!" 20 times on the same line (20 columns) over a duration of 20 seconds:

import time

for _ in range(20):
    print("Error!", end='', flush=True)
    time.sleep(1)  # change 1 to whatever number you want, to control the amount of time before "Error!" is printed again

Upvotes: 1

Related Questions