Typing effect in Python

前端 未结 7 1894
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 00:05

I want to make such program which reads characters from a string and prints each character after some delay so its look like typing effect.

Now my problem is sleep f

7条回答
  •  时光取名叫无心
    2021-01-13 00:42

    Long Answer:

    import sys
    import time
    
    variable_name = 'Words'
    
    for char in variable_name:
        time.sleep(0)
        sys.stdout.write(char)
    

    Why do you need the flush method? My code works without it.

    Short Answer:

    import time
    
    variable_name = 'Words'
    
    for char in variable_name:
        time.sleep(0)
        print(char, end = '')
    

    If you want the words to print vertically, then you should leave out the end argument.

    If you want the words to print horizontally, then you should use the end argument.

    Either way, the code works without the flush method.

提交回复
热议问题