如何增加延迟或等待print()。 (蟒蛇)

时间:2019-08-19 13:09:00

标签: python python-3.x

我目前正在开发基于文本的游戏,所以我希望增加文本显示的延迟。我要延迟的文本是if-else命令的第三个后代。

我已经尝试过time.sleep()函数,但这似乎不起作用。

 if path.lower().strip() == "yes":
                print("Then let your journey begin")
            else:
                print("\nWell,")

                print("You don't have much of a choice do you.") 
                # the text I need to delay ^^^


所以我希望做的是在被注释的文本的入口处添加几秒钟的延迟。

1 个答案:

答案 0 :(得分:4)

import time

# ...

if path.lower().strip() == "yes":
    print("Then let your journey begin")
else:
    print("\nWell,")
    time.sleep(3) # parameter used here is in seconds
    print("You don't have much of a choice do you.") 
    # the text I need to delay ^^^

time.sleep(secs)

  

secs-Python程序应暂停执行的秒数。此参数应为int或float。

     

在给定的秒数内暂停执行调用线程。该参数可以是浮点数,以指示更精确的睡眠时间。实际的暂停时间可能少于请求的暂停时间,因为任何捕获到的信号都会在执行该信号的捕获例程后终止sleep()。另外,由于系统中其他活动的安排,暂停时间可能比请求的时间长任意数量。

     

版本3.5中的更改:现在,即使睡眠被信号中断,该函数也将至少睡眠几秒钟,除非信号处理程序引发异常(有关原理,请参见PEP 475)。