簡體   English   中英

有沒有辦法讓 python 程序等待幾秒鍾? 我正在制作無限數量的打印機,但速度太快,無法讀取

[英]Is there a way to make a python program wait a few seconds? I am making an infinite number printer but it's too fast to read

我不知道如何讓我的程序等待幾秒鍾,這樣我才能真正閱讀它 python 中是否有等待函數或是否有此類模塊? 我沒有辦法在窗口中運行它,因為我在學校的 chromebook 上。

My Code:


from random import randint

while True:
    a = randint(0,999999)
    b = randint(0,999999)
    c = randint(0,999999)
    
    if (a <= b) or (a <= c):
        print("variable 'a' has been printed")
        print(a)
        
    elif (b <= a) or (b <= c):
        print("variable 'c' has been printed")
        print(b)
        
    elif (c <= a) or (c <= b):
        print("variable 'c' has been printed")
        print(c)
    
    elif (a == b):
        print("Combo of 'a' + 'b'")
        print(a + b)
        
    elif (a == c):
        print("Combo of 'a' + 'c'")
        print(a + c)
    
    elif (b == c):
        print("Combo of 'b' + 'c'")
        print(b + c)


How to make it wait?

使用 sleep(),它會在幾秒鍾內獲取參數。

from random import randint
from time import sleep
while True:
    a = randint(0,999999)
    b = randint(0,999999)
    c = randint(0,999999)

    
    if (a <= b) or (a <= c):
        print("variable 'a' has been printed")
        print(a)
        sleep(1)
        
    elif (b <= a) or (b <= c):
        print("variable 'c' has been printed")
        print(b)
        sleep(1)
        
    elif (c <= a) or (c <= b):
        print("variable 'c' has been printed")
        print(c)
        sleep(1)
    
    elif (a == b):
        print("Combo of 'a' + 'b'")
        print(a + b)
        sleep(1)
        
    elif (a == c):
        print("Combo of 'a' + 'c'")
        print(a + c)
        sleep(1)
    
    elif (b == c):
        print("Combo of 'b' + 'c'")
        print(b + c)
        sleep(1)

你需要像我上面那樣從時間導入它。

或者,如果在 while 循環的末尾只有一個 sleep() 而不是多個 sleep() 函數,它將更加高效和可維護。 下面是上面代碼的一個更好的例子。 感謝 Max 的建議。

from random import randint
from time import sleep
while True:
    a = randint(0,999999)
    b = randint(0,999999)
    c = randint(0,999999)


    if (a <= b) or (a <= c):
        print("variable 'a' has been printed")
        print(a)
    
    elif (b <= a) or (b <= c):
        print("variable 'c' has been printed")
        print(b)
    
    elif (c <= a) or (c <= b):
        print("variable 'c' has been printed")
        print(c)

    elif (a == b):
        print("Combo of 'a' + 'b'")
        print(a + b)
    
    elif (a == c):
        print("Combo of 'a' + 'c'")
        print(a + c)

    elif (b == c):
        print("Combo of 'b' + 'c'")
        print(b + c)

    sleep(1)

您可以在需要等待的任何地方在代碼中引入睡眠。

import time

time.sleep(5) # sleep for 5 seconds

鏈接到文檔time.sleep()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM