开发者

How to print a string with a little delay between the chars?

I want a text to be displayed as if it is just being typed. So I need a little delay after every letter.

I tried to do it this way:

import time

text = "Hello, this is a test text to see if all works fine."
for char in text:
   print char,time.sleep(0.2),

It works fine, except for one p开发者_JAVA技巧roblem. I get a "None" after every character.

This is the output:

H None e None l None l None o None , None   None t None h None i None s None   None i None s None   None a None   None t None e None s None t None   None t None e None x None t None   None t None o None   None s None e None e None   None i None f None   None a None l None l None   None w None o None r None k None s None   None f None i None n None e None . None

I don't know why this happens. I hope anyone can help me.


>>> import time
>>> import sys
>>> blah = "This is written slowly\n"
>>> for l in blah:
...   sys.stdout.write(l)
...   sys.stdout.flush()
...   time.sleep(0.2)
...
This is written slowly


You are printing the result of time.sleep(0.2), which is None. Move it to the next line.

text = "Hello, this is a test text to see if all works fine."
for char in text:
    print char,
    time.sleep(0.2)

Of course, you still have the problem of a space between each character, which can be solved by replacing the print statement with a call to sys.stdout.write.

text = "Hello, this is a test text to see if all works fine."
for char in text:
    sys.stdout.write(char)
    time.sleep(0.2)


Put the time.sleep in a separate line. With a comma, you are printing its return value as well.


You're printing the return value of time.sleep(0.2) which is None. Put it on a separate line. The comma after "print char" will prevent a newline from being printed but it will introduce a single space after each character.

Try this instead:

>>> import sys
>>> import time
>>> text = "Hello, this is a test text to see if all works fine."
>>> for char in text:
...     sys.stdout.write(char)
...     time.sleep(0.2)


Your example prints them all on separate lines I think (at least on windows). You can use printing to sys.stdout to get around this.

import time, sys
for character in text:
    sys.stdout.write(character)
    time.sleep(0.2)


this line:

print char, time.sleep(0.2)

decodes as "print the value of char, and then print the return value of the function time.sleep() (which is None)".

You can break them onto separate lines, but the default behavior of print followed by a comma will leave you with spaces between the characters that you probably don't want. If not, look up how to change the behavior of print, or do something like this:

>>> import sys
>>> import time
>>> for char in "test string\n":
...    sys.stdout.write(char)
...    time.sleep(0.2)
...
test string
>>>


Thank you all for your help, this is my final code, I made a random timing for the delay as mentioned by Wooble:

import time
import sys
from random import randrange

text = "This is the introduction text."

for c in text:
    sys.stdout.write(c)
    sys.stdout.flush()
    seconds = "0." + str(randrange(1, 4, 1))
    seconds = float(seconds)
    time.sleep(seconds)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜