【问题标题】:python: Timer with a input time and input intervalpython:具有输入时间和输入间隔的计时器
【发布时间】:2022-01-13 16:25:42
【问题描述】:

感谢您查看我的问题。

我对 python 完全陌生,在编写带有输入和输入间隔的计时器时遇到了一些麻烦。 我正在尝试使用可自定义的时间间隔来自定义打印消息的时间长度。 有人可以帮忙吗?

【问题讨论】:

    标签: python input timer


    【解决方案1】:

    鉴于您的问题以及您在 cmets 中向我解释的内容,以下代码将解决您的问题:

    import threading
    import time
    
    y = int(input("interval: "))
    x = int(input("time: "))
    running = True
    
    def countdown(x):
        while x > 0:
            print(x)
            x -= 1
            time.sleep(1)
    
    def sound(y):
        while running is True:
            time.sleep(y)
            print("interval test")
    
    sound_thread = threading.Thread(target=sound, args=(y,))
    sound_thread.start()
    
    countdown_thread = threading.Thread(target=countdown, args=(x,))
    countdown_thread.start()
    countdown_thread.join()
    
    running = False
    print("done")
    

    【讨论】:

    • 当我输入我的间隔值并按时间运行 eksampel 10 和间隔 2 时,没有每 2 秒打印一次“间隔测试”。那是我的主要问题。顺便谢谢。
    • 如果我将间隔设置为 2 并将计时器设置为 10,则 id 喜欢它为 10、9、间隔测试、8、7、间隔测试等。但恐怕我不知道该怎么做。
    • 好的,知道了!您可以为sound(y)countdown(x) 创建两个线程,它们将同时运行,当countdown(x) 完成时,您可以发送exit(1) 来完成您的应用程序。
    • 很抱歉问了这么多,但会是这样吗?:countdown_thread = threading.Thread(target = countdown, args=(x,)) countdown_thread.start() sound_thread = threading .Thread(target = sound, args=(y,)) sound_thread.start() 我对此很不确定,抱歉
    • 非常感谢,我真的学到了很多。你是我的英雄!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2022-10-14
    相关资源
    最近更新 更多