Python 打印一个字符串,在字符之间有一点延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4627033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing a string with a little delay between the chars
提问by Black-Pixel
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 problem. 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
H无e无l无l无o无,无无t无h无i无s无无i无s无无a无无t无e无s无t无无t无e无x无t无无t无o 无无 s 无 e 无 e 无 i 无 f 无无 a 无 l 无 l 无无 w 无 o 无 r 无 k 无 s 无无 f 无 i 无 n 无 e 无。没有任何
I don't know why this happens. I hope anyone can help me.
我不知道为什么会发生这种情况。我希望任何人都可以帮助我。
采纳答案by MattH
>>> 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
回答by Aphex
Put the time.sleepin a separate line. With a comma, you are printing its return value as well.
将 放在time.sleep单独的行中。使用逗号,您也正在打印其返回值。
回答by Steven Rumbalski
You are printing the result of time.sleep(0.2), which is None. Move it to the next line.
您正在打印 的结果time.sleep(0.2),即None。将其移至下一行。
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 printstatement with a call to sys.stdout.write.
当然,你仍然有每个字符之间有空格的问题,可以通过print调用替换语句来解决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)
回答by Jeremy Brown
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.
您正在打印 time.sleep(0.2) 的返回值,即 None。把它放在一个单独的行上。“print char”之后的逗号将阻止打印换行符,但它会在每个字符后引入一个空格。
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)
回答by aodj
Your example prints them all on separate lines I think (at least on windows). You can use printing to sys.stdoutto get around this.
您的示例将它们全部打印在我认为的单独行上(至少在 Windows 上)。您可以使用打印来sys.stdout解决此问题。
import time, sys
for character in text:
sys.stdout.write(character)
time.sleep(0.2)
回答by bgporter
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)".
解码为“打印 的值char,然后打印函数的返回值time.sleep()(即None)”。
You can break them onto separate lines, but the default behavior of printfollowed 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:
您可以将它们分成单独的行,但print后跟逗号的默认行为会在您可能不想要的字符之间留下空格。如果没有,请查看如何更改 的行为print,或执行以下操作:
>>> import sys
>>> import time
>>> for char in "test string\n":
... sys.stdout.write(char)
... time.sleep(0.2)
...
test string
>>>
回答by Black-Pixel
Thank you all for your help, this is my final code, I made a random timing for the delay as mentioned by Wooble:
谢谢大家的帮助,这是我的最终代码,我为 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)

