在循环中获取第二个值

Get 2nd value in loop

目前正在编写电报机器人以从 Raspberry Pi 上的 Envrio+ phat 获取实时数据...

当您 运行 内置示例时,它将始终打印以下内容:

The current tempurature is 21.70818430223153 *C 

The current pressure is  700.2708792437015hPa

The Current Humidty is 84.54408663293306 %

在 while 循环的下一次打印中,它将为您提供正确的环境数据。为了解决这个问题,我只需要获取第二组数据而不是第一组。

我试过在 while 循环中缩进变量,但这在机器人中不起作用并且碰壁了。

以下是机器人中的工作原理,但每次获取天气状态时都会获取与上面发布的相同的值。

def weather(bot, update):
    chat_id = update.message.chat_id
    temp = bme280.get_temperature()
    pressure = bme280.get_pressure()
    humidity = bme280.get_humidity()
    current_temp = "The current tempurature is " + str(temp)  + " *C " + "\nThe current pressure is  " +  str(pressure) + "hPa" + "\nThe Current Humidty is " + str(humidity) + " %"
    bot.send_message(chat_id=chat_id, text=current_temp)

while 循环仍然带来相同的值,而不是示例循环中的下一个值。

如有任何帮助,我们将不胜感激

这听起来像是第一个请求或第一个循环初始化机器人并返回默认值,然后你得到真正的值。

也许这两种策略中的一种可行:

1.) Send a single request first and ignore the result, thereafter do a while loop
2.) Initialise a counter before the while loop and if counter = 1 then ignore results and loop

更多代码可能有助于缩小答案范围。

在过去的几天里,我能够解决这个问题,因为我还在学习 python,所以花了一点时间,但之前是工作代码:

while True: 
    if counter < 2:
        temperature = bme280.get_temperature()
        pressure = bme280.get_pressure()
        humidity = bme280.get_humidity()
        counter = counter + 1
        time.sleep(1)
    else:
        print("""Temperature: {:05.2f} *C
    Pressure: {:05.2f} hPa
    Relative humidity: {:05.2f} %
    """.format(temperature, pressure, humidity))
        break