Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-用户输入n秒后重复函数_Python_Python 3.x - Fatal编程技术网

Python-用户输入n秒后重复函数

Python-用户输入n秒后重复函数,python,python-3.x,Python,Python 3.x,我的任务是创建一个程序,当室内温度低于16°C时,该程序将打开加热器;当室内温度高于16°C时,该程序将关闭加热器。我决定让它变得有用,并导入计时器。我想知道,在用户输入“n”时间后,如何重复允许打开或关闭加热器的功能。 我目前的代码是: import time import random def main(): temp = random.randint(-15, 35) print("Current temperature in the house:&

我的任务是创建一个程序,当室内温度低于16°C时,该程序将打开加热器;当室内温度高于16°C时,该程序将关闭加热器。我决定让它变得有用,并导入计时器。我想知道,在用户输入“n”时间后,如何重复允许打开或关闭加热器的功能。 我目前的代码是:

import time
import random


def main():
    
    temp = random.randint(-15, 35)
    print("Current temperature in the house:", temp,"°C")
    time.sleep(1)

    if temp <= 16:
        print("It's cold in the house!")
        t = input("How long should the heating work? Enter time in 1.00 (hours.minutes) format:")
        print("Heating will work for:", t)
        print("House Heating status: ON")
        time.sleep() //The timer should start here for the time entered by the user
        
        
        
    if temp > 16 and temp <= 25:
        print("House Heating status: OFF")
    if temp => 26:
        print("House Cooler status: ON")


main()

导入时间
随机输入
def main():
温度=random.randint(-15,35)
打印(“室内当前温度:,温度,°C”)
时间。睡眠(1)
如果温度为16和26:
打印(“内部冷却器状态:打开”)
main()

我应该使用哪种技术来添加此计时器?

假设您的
main
函数已经处理了对
time.sleep的调用,一种反复重复的简单方法是将函数放入无限循环中:

为True时:
main()
另一种方法是让
main
函数返回一个整数,该整数表示在再次调用该函数之前等待的时间。这将等待与主逻辑分离

def main():
...
t=输入(“加热工作多长时间?以1.00(小时.分钟)格式输入时间:”)
...
返回整数(t)
尽管如此:
等待时间=main()
时间。睡眠(等待时间)

您可以使用while true循环,然后在内部使用
time.sleep(n)