Python 打印速度慢-仍然需要答案
这个话题已经讨论过了,但我仍然有困难。我的代码确实打印字符串,但打印速度非常快。我尝试过改变时间值,但似乎什么都没有改变 这是我所有的一切-也许我只需要一个更完整的例子Python 打印速度慢-仍然需要答案,python,python-2.7,Python,Python 2.7,这个话题已经讨论过了,但我仍然有困难。我的代码确实打印字符串,但打印速度非常快。我尝试过改变时间值,但似乎什么都没有改变 这是我所有的一切-也许我只需要一个更完整的例子 import sys,time,random typing_speed = 50 #wpm def slow_type(t): for l in t: sys.stdout.write(l) time.sleep(random.random()*10.0/typing_speed) pr
import sys,time,random
typing_speed = 50 #wpm
def slow_type(t):
for l in t:
sys.stdout.write(l)
time.sleep(random.random()*10.0/typing_speed)
print "Tell if you think this is too fast?"
我更改了函数的名称,因为在我看来,slowprint
似乎更明确一些
我更喜欢下面的版本-它接受一个参数来控制速度
import sys,time,random
def slowprint(t, s): # s is the typing speed - formally held in `typing_speed`
for l in t:
sys.stdout.write(l)
sys.stdout.flush() # Forcing the output of everything in the buffer.
time.sleep(random.random()*10.0/s)
sys.stdout.write("\n") # Printing a newline, because it looks cleaner.
slowprint ("Tell if you think this is too fast?", 50)
我更改了函数的名称,因为在我看来,slowprint
似乎更明确一些
我更喜欢下面的版本-它接受一个参数来控制速度
import sys,time,random
def slowprint(t, s): # s is the typing speed - formally held in `typing_speed`
for l in t:
sys.stdout.write(l)
sys.stdout.flush() # Forcing the output of everything in the buffer.
time.sleep(random.random()*10.0/s)
sys.stdout.write("\n") # Printing a newline, because it looks cleaner.
slowprint ("Tell if you think this is too fast?", 50)
我对现有解决方案的问题是,我看不出代码是如何强制输出到指定的WPM的,除非通过反复试验:
time.sleep(random.random()*10.0/typing_speed)
从时间上看,似乎是弄错了,速度快了2倍多
相反,如果我们在WPM中使用单词的标准定义为“五次击键”,在WPM中使用分钟的标准定义为“六十秒”,那么“告诉你是否认为这太快?”应该需要大约8.5秒。使用上述逻辑重新编写代码,我们得到如下结果:
import sys
import time
typing_speed = 50 # wpm
def slowprint(string):
# the WPM system considers a word to be 5 keystrokes
# so convert from words per minute to seconds per character
spc = 12.0 / typing_speed # 12.0 = 60 seconds per minute / 5 characters per word
for character in string:
sys.stdout.write(character)
sys.stdout.flush() # Force the output of everything in the buffer.
time.sleep(spc)
sys.stdout.write("\n") # print a newline, because it looks cleaner.
slowprint("Tell if you think this is too fast?")
我对现有解决方案的问题是,我看不出代码是如何强制输出到指定的WPM的,除非通过反复试验:
time.sleep(random.random()*10.0/typing_speed)
从时间上看,似乎是弄错了,速度快了2倍多
相反,如果我们在WPM中使用单词的标准定义为“五次击键”,在WPM中使用分钟的标准定义为“六十秒”,那么“告诉你是否认为这太快?”应该需要大约8.5秒。使用上述逻辑重新编写代码,我们得到如下结果:
import sys
import time
typing_speed = 50 # wpm
def slowprint(string):
# the WPM system considers a word to be 5 keystrokes
# so convert from words per minute to seconds per character
spc = 12.0 / typing_speed # 12.0 = 60 seconds per minute / 5 characters per word
for character in string:
sys.stdout.write(character)
sys.stdout.flush() # Force the output of everything in the buffer.
time.sleep(spc)
sys.stdout.write("\n") # print a newline, because it looks cleaner.
slowprint("Tell if you think this is too fast?")
在您的示例中,定义一个函数并打印一个字符串。当然会很快。您至少应该在打印try
slow\u type(“告诉您是否认为这太快了?”)
之前调用您的函数。您从未使用过slow\u type
!在您的示例中,您定义了一个函数并打印了一个字符串。当然会很快。您至少应该在打印tryslow\u type(“告诉您是否认为这太快了?”)
之前调用您的函数。您从未使用过slow\u type
!每次写完后你都忘了冲洗。谢谢@jwpfox。谢谢@jwpfox。