【问题标题】:How to make this function slow for certain lines?如何使某些行的此功能变慢?
【发布时间】:2014-09-20 03:38:18
【问题描述】:

好的,我制作了一个慢速打印脚本,我很无聊,所以我想尝试让它看起来很酷。 现在,当我这样做时,它会以一种速度打印所有延迟的打印块。我想做的是让一些线路以不同的速度运行。例如“正在加载:||||||||||”写完需要 5 秒,而“祝你有美好的一天”需要 0.05 秒。

这是我的代码。 # -- 编码:utf-8 -- 进口时间 导入系统

def delay_print(s):
    for c in s:
        sys.stdout.write( '%s' % c )
        sys.stdout.flush()
        time.sleep(0.01)

delay_print("Hello!")
delay_print("Please wait while this loads :) ")
delay_print(Loading: ||||||||||") #The line that I want to make slower than the rest
delay_print("Thanks!") #return to regular speed.

【问题讨论】:

  • '%s' % c 对字符串没有任何作用,应该只是 c

标签: python performance printing


【解决方案1】:

也许这就是你想要的:

def delay_print(s, t):
    for c in s:
        sys.stdout.write( '%s' % c )
        sys.stdout.flush()
        time.sleep(t)

delay_print("Hello!", 0.5)
delay_print("Please wait while this loads :) ", 0.5)
delay_print(Loading: ||||||||||", 5)
delay_print("Thanks!", 0.5)

【讨论】:

    猜你喜欢
    • 2019-10-16
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多