Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8041

MicroPython • Re: Weird modulus problem

$
0
0
The `time.localtime()` function gives you the exact time when you call it, so in the `if` statement it's probably getting called right at the end of the 14:30:59 second for the modulo 2 part (which is %2 so that returns true), then called again at the start of the 14:31:00 second for the equals 0 part (which is at 0 seconds, so the second part also returns true)

To fix this, you should only call `time.localtime()` once just before the `if` statement and store the result in a variable, and then you can use that variable in the `if` statement

Code:

import timewhile True:    now = time.localtime()    if (now[4] % 2) == 0 and now[5] == 0:        print ("{:02d}:{:02d}:{:02d} - {:02d}".format(now[3], now[4], now[5], now[4] % 2))        time.sleep (1)

Statistics: Posted by will-v-pi — Fri May 02, 2025 4:47 pm



Viewing all articles
Browse latest Browse all 8041

Trending Articles