Here is the code, delay=n < this n is giving me a warning.
n = {1: .01, 2: .03, 3: .05, 4: .07, 5: .09, 6: .11, 7: .13, 8: .15, 9: .17, 10: .19,
11: .21, 12: .23, 13: .25, 14: .27, 15: .29, 16: .31, 17: .33}
def slow_print(x, delay=n):
for letter in x:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(delay=n)
And here's the code that someone in chat helped me with, it works great! His name is PM 2Ring.
def slow_print(text, speed=1):
delay = (2 * speed - 1) / 100
for letter in text:
print(letter, end="", flush=True)
time.sleep(delay)
CodePudding user response:
Try something like:
def slow_print(x):
i = 0
for letter in x:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(n[i])
i = 1
Actually, you do not need a dictionary for delays, just a list. And be sure that the number of letters in the argument is equal to the number of delays 1.
Maybe it is better to generate random delays.
CodePudding user response:
A truncated, normally distributed random delay value would be a great choice, as it would allow you to set the average speed of the message being "typed" but will also include a little bit of "jitter" to make the typing feel more natural.
You'll have to play around with the distribution to get something you like, but it'll give you the control you want.
import random
def delayed_print(message: str, *, mean: float, variance: float):
for char in message:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(max(0, random.gauss(mean, variance)))
Examples:
# Prints fast
delayed_print("hello world", mean=0.01, variance=0.01)
# Prints slow
delayed_print("hello world", mean=0.5, variance=0.1)
# Prints fast sometimes and slow other times
delayed_print("hello world", mean=0.25, variance=0.5)
From here, you could construct a dictionary with some saved values that you could reuse, so you don't have to retype the mean
and variance
parameters every time you want to print:
slow_type = dict(mean=0.5, variance=0.1)
fast_type = dict(mean=0.01, variance=0.01)
# Prints fast
delayed_print("hello world", **fast_type)
CodePudding user response:
Here is slow_print
with import delays.
I supposed thаt maximum 2.0 seconds delay is good enough, but feel free to change this value/
import random
def slow_print(x, max_delay = 2.0):
delay = max_delay * random.random()
for letter in x:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(delay)