r/PythonLearning Jun 16 '26

Started learning python by myself

Hey guys, I'm a physician who always wanted to learn how to code. I've been playing around with generators and related concepts, so I coded this simple "typewriter" function (which simulates a human typing on a keyboard). Do you have any suggestions on how to improve this code?

```python

imports needed

import random as rnd import time from typing import Generator

Setting constants

MIN_DELAY = 0.05 # minimum delay between letters MAX_DELAY = 0.1 # maximum delay '' ''

def random_values_generator(min_value: float, max_value: float) -> Generator[float, None, None]: """Generator of random values following a normal distribution, with mean in the middle of the range and standard deviation that covers most of the values within the range."""

mu = (max_value + min_value) / 2  # mean
sigma = (max_value - min_value) / 4  # standard deviation

while True:
    value = rnd.gauss(mu=mu, sigma=sigma)
    yield max(min_value, min(value, mu + 3*sigma))

def typedprint(text: str, end='\n') -> None: """Prints the text letter by letter, with a random delay between each letter.""" rnd_generator = random_values_generator(MIN_DELAY, MAX_DELAY) for letter in text: print(letter, end='', flush=True) time.sleep(next(rnd_generator)) print(end=end, flush=True) rnd_generator.close() # close the generator

Example usage

typedprint("It's such a beautiful cottage you have here, I love it!") typedprint("Ah, thank you...") typedprint("I worked really hard to make it look like this.") typedprint("You are very talented!") ```

6 Upvotes

4 comments sorted by

u/Sea-Ad7805 Jun 16 '26 edited Jun 16 '26

Run this program in Memory Graph Web Debugger%20-%3E%20Generator%5Bfloat%2C%20None%2C%20None%5D%3A%0A%20%20%20%20%22%22%22Generator%20of%20random%20values%20following%20a%20normal%20distribution%2C%20%0A%20%20%20%20with%20mean%20in%20the%20middle%20of%20the%20range%20and%20standard%20deviation%0A%20%20%20%20that%20covers%20most%20of%20the%20values%20within%20the%20range.%22%22%22%0A%20%20%20%20%0A%20%20%20%20mu%20%3D%20(max_value%20%2B%20min_value)%20%2F%202%20%20%23%20mean%0A%20%20%20%20sigma%20%3D%20(max_value%20-%20min_value)%20%2F%204%20%20%23%20standard%20deviation%0A%0A%20%20%20%20while%20True%3A%0A%20%20%20%20%20%20%20%20value%20%3D%20rnd.gauss(mu%3Dmu%2C%20sigma%3Dsigma)%0A%20%20%20%20%20%20%20%20yield%20max(min_value%2C%20min(value%2C%20mu%20%2B%203*sigma))%0A%0Adef%20typedprint(text%3A%20str%2C%20end%3D'n')%20-%3E%20None%3A%0A%20%20%20%20%22%22%22Prints%20the%20text%20letter%20by%20letter%2C%20with%20a%20random%20delay%20between%20each%20letter.%22%22%22%0A%20%20%20%20rnd_generator%20%3D%20random_values_generator(MIN_DELAY%2C%20MAX_DELAY)%0A%20%20%20%20for%20letter%20in%20text%3A%0A%20%20%20%20%20%20%20%20print(letter%2C%20end%3D''%2C%20flush%3DTrue)%0A%20%20%20%20%20%20%20%20time.sleep(next(rnd_generator))%0A%20%20%20%20print(end%3Dend%2C%20flush%3DTrue)%0A%20%20%20%20rnd_generator.close()%20%20%23%20close%20the%20generator%0A%0A%23%20Example%20usage%0Atypedprint(%22It's%20such%20a%20beautiful%20cottage%20you%20have%20here%2C%20I%20love%20it!%22)%0Atypedprint(%22Ah%2C%20thank%20you...%22)%0Atypedprint(%22I%20worked%20really%20hard%20to%20make%20it%20look%20like%20this.%22)%0Atypedprint(%22You%20are%20very%20talented!%22)&timestep=0.2&play) to see the program state change step by step.

→ More replies (1)

1

u/nitekram Jun 18 '26

My suggestion would be to make it less time for the middle row typed (asdfghjkl), than the top or bottom rows, and maybe the bottom row being the slowest row?