0

我当前的 if 语句没有按我希望的那样工作,它们应该在某些时候更改变量,但变量似乎没有改变......

import random
import datetime
from sched import scheduler
from time import time, sleep, strftime

s = scheduler(time, sleep)
random.seed()

def run_periodically(start, end, interval, func):
    event_time = start
    while event_time < end:
        s.enterabs(event_time, 0, func, ())
        event_time += interval + random.randrange(-5, 10)
    s.run()

clearscreen = "false"

def clear():
    global clearscreen
    currenttime = strftime('%H:%M:%S')
    print currenttime


## When currenttime is in range, it should change clearscreen from false to true, but it doesn't
    if currenttime > "00:45:00" and currenttime < "00:46:00":
        print "Time Range 1 Working"
        if clearscreen == "false":
            print "Clear screen is set to false"
            clearscreen == "true"

## Same here, when currenttime is in range it should change clearscreen from true to false.
    if currenttime > "00:46:00" and currenttime < "00:47:00":
        print "Time Range 2 Working"
        if clearscreen == "true":
            print "clearscreen is set to true"
            clearscreen == "false"
            print "Cleared Screen"

getData()

run_periodically(time()+5, time()+1000000, 10, clear)

任何人都知道我做错了什么,也无需提及使用UTC时间。

谢谢-Hyflex

4

1 回答 1

6

clearscreen == "true"两个=s 不会改变 的值clearscreen。您需要在块内(即,不在行本身上)编写clearscreen = "true"(以及以后clearscreen = "false") 。ifif

于 2013-06-19T23:58:59.033 回答