datetime模块不匹配int与int的比较

时间:2017-02-28 21:17:24

标签: python

我目前正在制作一个闹钟,作为进一步学习python3的工具,我遇到了一个问题。

def clockCheck():
    #get the hour and time from setAlarm
    splitTime = re.compile(r'(\d+?)(:)(\d+)')
    timeRe = splitTime.search(setAlarm())
    alarmHour = timeRe.group(1)
    alarmMinute = timeRe.group(3)

    #get the live time
    now = datetime.datetime.now()
    currentHour = now.hour
    currentMinute = now.minute
    currentSecond = now.second

    print(currentHour)
    print(alarmHour)

    while True:
        if currentHour != alarmHour:
            print("check1")
            time.sleep(60-currentSecond) #if this isn't done, the alarm could be off by a couple seconds. Line's up things
            time.sleep((60*60) - (60*currentMinute)) #this sleeps the program until the next hour is hit and reruns a check
        elif currentMinute != alarmMinute:
            print("check2")
            time.sleep(60-currentSecond) #sleep until the next minute and rerun the check

            #play sound
            #break out of program, it's done

在这段代码片段中,当currentHour与alarmHour(第一个if语句)进行比较时,即使这两个变量相同,代码也会执行if语句,它们彼此不相等。

换句话说,如果currentHour = 1且alarmHour = 1,则代码将像currentHour!= alarmHour一样执行,并将运行if语句代码。 显然这没有意义,我不知道为什么在明显1 == 1时会发生这种情况。我已经发布了完整的代码,随意玩它。所有这些模块都内置在python3.6

这是我的完整代码

'''
alarmclock.
ask user for what time to blare off
compare current time to set alarm time
if they do not equal the same then sleep the program for x time and `    rerun the check where it checks if current time is equal alarm time yet. loop until currenttime equals alarm time. then play soun`d

'''
import time
import datetime
import re

def setAlarm():
    print("Hello, what time would you like the alarm to sound? Please input in this format\ne.g. 7:45pm\n")
    time = input("Time: ")

    splitTime = re.compile(r'(\d+?)(:)(\d+?)(pm|am)') #split up time inputted for manipulation
    timeRe = splitTime.search(time)

    hour = int(timeRe.group(1))
    minutes = int(timeRe.group(3))
    dayOfTime = timeRe.group(4).lower() #set pm or am to lowercase for ease

    #errorChecking for proper time format
    if hour > 12 or hour < 1:
        print("Please input your time properly, in 12 hour time")
        setAlarm()

    if minutes > 59 or minutes <  0:
        print("Please input your time properly")
        setAlarm()


    #if time of day is pm, then reassign all values from 1pm - 11:59pm as 13, 14, 15, etc 24 hour bullshit.
    if dayOfTime == "pm" and hour != 12:
        convertedHour = hour + 12

    else:
        convertedHour = hour

    if dayOfTime == "am" and hour == 12:
        convertedHour = 24

    finalTime = str(convertedHour) + ":" + str(minutes)
    print(finalTime)
    return finalTime



def clockCheck():
    #get the hour and time from setAlarm
    splitTime = re.compile(r'(\d+?)(:)(\d+)')
    timeRe = splitTime.search(setAlarm())
    alarmHour = timeRe.group(1)
    alarmMinute = timeRe.group(3)

    #get the live time
    now = datetime.datetime.now()
    currentHour = now.hour
    currentMinute = now.minute
    currentSecond = now.second

    print(currentHour)
    print(alarmHour)

    while True:
        if currentHour != alarmHour:
            print("check1")
            time.sleep(60-currentSecond) #if this isn't done, the alarm could be off by a couple seconds. Line's up things
            time.sleep((60*60) - (60*currentMinute)) #this sleeps the program until the next hour is hit and reruns a check
        elif currentMinute != alarmMinute:
            print("check2")
            time.sleep(60-currentSecond) #sleep until the next minute and rerun the check

            #play sound
            #break out of program, it's done


def main():
    clockCheck()

main()

0 个答案:

没有答案