printing slowly (Simulate typing)

前端 未结 4 1154
悲哀的现实
悲哀的现实 2020-12-05 21:35

I am trying to make a textual game in python. All goes well however, I would like to make a function that will allow me to print something to the terminal, but in a fashion

相关标签:
4条回答
  • 2020-12-05 22:06

    Try this:

    import sys,time
    
    def sprint(str):
       for c in str + '\n':
         sys.stdout.write(c)
         sys.stdout.flush()
         time.sleep(3./90)
    
    sprint('hello world')
    
    0 讨论(0)
  • 2020-12-05 22:16

    In Python 2.x you can use sys.stdout.write instead of print:

    for letter in str:
        sys.stdout.write(letter)
        time.sleep(.1)
    

    In Python 3.x you can set the optional argument end to the empty string:

    print(letter, end='')
    
    0 讨论(0)
  • 2020-12-05 22:17

    This is my "type like a real person" function:

    import sys,time,random
    
    typing_speed = 50 #wpm
    def slow_type(t):
        for l in t:
            sys.stdout.write(l)
            sys.stdout.flush()
            time.sleep(random.random()*10.0/typing_speed)
        print ''
    
    0 讨论(0)
  • 2020-12-05 22:19

    Try this:

    def print_slow(str):
        for letter in str:
            sys.stdout.write(letter)
            sys.stdout.flush()
            time.sleep(0.1)
    
    print_slow("Type whatever you want here")
    
    0 讨论(0)
提交回复
热议问题