1

I'm trying to create a program, where it types whatever needs to be typed, and sometimes it makes a mistake which it corrects, as if an actual person is typing, here is my code:

import time
import sys
import random
import string 
import uuid

def my_random_string(string_length=4):
    random = str(uuid.uuid4())
    random = random.lower()
    random = random.replace("-","")
    return random[0:string_length]

def delay_print(s):
    for c in s:
        sys.stdout.write( '%s' % c )
        sys.stdout.flush()
        time.sleep(random.uniform(0.01,0.5))
        random_string = random.randint(0,6)
        if random_string == 5:
            sys.stdout.write(my_random_string(2))
            time.sleep(0.1)
            #delete the two characters

delay_print("Hello, and welcome to the Enrichment Centre")

I don't know how to delete the two characters that I placed.

Max
  • 729
  • 5
  • 15

1 Answers1

1

The backspace character \b will go back before your previous character, however it then needs to be typed over to actual go blank. The easiest way to do that is to then enter a space. However you're then spaced in one character too far, so you need another backspace to go before the space. And you want to loop over this happening twice to actually remove both of the random characters. This is what I did to make it most convincingly look like someone typing:

    if random_string == 5:
        sys.stdout.write(my_random_string(2))
        sys.stdout.flush()
        time.sleep(0.1)
        for _ in range(2):
            sys.stdout.write('\b \b')
            sys.stdout.flush()
            time.sleep(0.1)
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73