2013-06-19 46 views
0

我當前的if語句不工作,因爲我希望他們,他們應該在某些時候更改變量,但該變量似乎並沒有改變..Python 2.7,如果語句被忽略,而不是設置/更改

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

+0

是否有一個你使用'「true」'和'「false」'而不是'True'和'False'的理由? – SethMMorton

回答

6

clearscreen == "true"有兩個= s不改變clearscreen值。您需要在if塊內寫入clearscreen = "true"(和更晚的clearscreen = "false")(而不是在if行本身)。

+0

啊,這麼小的一個錯誤。什麼==改變它,我怎麼沒有得到一個語法錯誤? – Ryflex

+0

@Hyflex:'=='是一個比較運算符,如果它的操作數相等則返回'True',否則返回'False'。 '='用於變量賦值。 – jwodder