1

我正在为我的小温室制作一个 python 程序来测试不同的条件。我需要它做的是:程序结束时,等待 5 分钟,然后再次运行所有测试。这是我第一个完整的 python 程序,所以如果你能把答案保留在低学习曲线区域,那将是非常有用的。

例子:

temp=probe1  
humidity=probe2  
CO2=probe3  

if temp==25:  
      print ("25)"  
if humidity==90:  
      print ("90")  
if CO2==1000  
      print ("1000")  

import time  
time.sleep (300) 
start again from top until keypress
4

1 回答 1

2

您可以将您现在拥有的内容放在一个while True 循环中以实现所需的结果。这将永远运行,每 5 分钟进行一次测量,直到您按 Ctrl-C 中断程序。

import time
while True:
    temp=probe1  
    humidity=probe2  
    CO2=probe3  

    if temp==25:  
          print ("25") 
    if humidity==90:  
          print ("90")  
    if CO2==1000  
          print ("1000")    
    time.sleep (300) 

但是,我想知道您的传感器准确给出您检查的值的可能性有多大。根据传感器值的精度,您可能会在数小时甚至更长时间内没有任何输出。您可能想要检查四舍五入的传感器值,例如if round(temp) == 25

或者,也许您想知道什么时候temp是 25 或更高,您可以使用if temp >= 25.

另一种可能性是始终打印传感器数据,并在值高于某个阈值的情况下打印额外警告,例如:

import time
while True:
    temp=probe1  
    humidity=probe2  
    CO2=probe3  

    print("Temp:", temp, "degrees")
    if temp>=25:  
          print ("  Too hot!")  

    print("Humidity:", humidity, "%")
    if humidity>=90:  
          print ("  Too humid!")  

    print("CO2:", CO2, "units")
    if CO2>=1000  
          print ("  Too much CO2!")    
    time.sleep (300) 
于 2013-02-14T19:27:59.387 回答