2

if someone could help with my problem i would really be grateful, I'm not an expert when it comes to python 3 but I know a thing or too since I'm learning.

My problem is I'm trying to have text display on a python program in the command line that looks as if it's being typed in real time by a user, I know this works in pythons shell but wont,t in CMD.

I've used CX_freeze to develop the script to a .exe, I've looked around but haven't had any luck finding someone with a similar problem.

Import sys  
import time as t

def typing(sentence):
    for words in sentence+"\n":
        sys.stdout(words)
        t.sleep(0.04765) **Ignore the silly float, me being a perfectionist**

Then i delay and call the function, "text" as an example because why not..

t.sleep(2)
typing("Text")

And then there should be a small delay in typing the word

but if this can not be achieved in cmd, please can anyone tell me if there is another default application in windows that can be used as a shell?

AJ X.
  • 2,729
  • 1
  • 23
  • 33
killerbee
  • 21
  • 2

1 Answers1

1

So the issue here is to dynamically update the screen, you need to use sys.stdout.flush(). Also, sys.stdout is not a function, you need to use sys.stdout.write("")

Note: In the example function below, I also used random lib to add a more human look to the typing:

import sys
import time
import random
def typing(sentence):
    for words in sentence+"\n":
        sys.stdout.write(words)
        sys.stdout.flush()
        time.sleep(0.45*random.random())

EDIT: just a quick disclaimer, I don't own a windows machine, so I haven't tested this on cmd, but it does work on OSX Terminal

Raymond
  • 31
  • 7