SirDurtle
SirDurtle

Reputation: 5

Trying to make a program that simulates typing

I'm trying to making a program where each keypress prints the next character in a predetermined string, so it's like the user is typing text.

Here's the code I'm trying to use:

def typing(x):
  letter = 0
  for i in range(0, len(x)):
    getch.getch()
    print(x[letter], end = "")
    letter += 1
    
typing("String")

What happens here is you need to press 6 keys (The length of the string) and then it prints all at once. I can sort of fix this by removing the , end = "", which makes the letters appear one at a time, but then the outcome looks like this:

S
t
r
i
n
g

Any ideas for making the letters appear one at a time and stay on the same line?

Upvotes: 0

Views: 237

Answers (1)

DriftAsimov
DriftAsimov

Reputation: 413

You can try this code which works for me:

import time

def typewrite(word: str):
    for i in word:
        time.sleep(0.1)
        print(i, end="", flush = True)

typewrite("Hello World")

Upvotes: 2

Related Questions