4d5050
4d5050

Reputation: 43

Python - Alarm clock with multiple alarms

I'm trying to create an alarm clock with multiple alarms, which are set from a text file with a specific time each line, but at the moment it's only working if I set only one time. What am I doing wrong? Here is the code I have right now:

import time
from playsound import playsound

Time = time.strftime('%H:%M')

with open('alarms.txt') as f:
    alarms = f.readlines()
    for alarm in alarms:
        while Time != alarm:
            print('The time is: ' + Time)
            Time = time.strftime('%H:%M')
            time.sleep(1)
            continue
        if Time == alarm:
            playsound('alarm.mp3')

And my alarms.txt is setup like HH:MM: 18:45 18:55

Upvotes: 1

Views: 2085

Answers (1)

Chris Clayton
Chris Clayton

Reputation: 251

From the information given so far, my thought is this:

Remember that readlines() reads in lines and returns strings with the newline character still trailing. You need to compare the Time to the lines with the newline character removed.

import time
from playsound import playsound

Time = time.strftime('%H:%M')

with open('alarms.txt') as f:
    alarms = f.readlines()
    for alarm in alarms:
        alarm = alarm.rstrip('\n')
        while Time != alarm:
            print('The time is: ' + Time + '\n')
            Time = time.strftime('%H:%M')
            time.sleep(1)
            continue
        if Time == alarm:
            playsound('alarm.mp3')

This also assumes, of course, that the times in the text file are in the desired chronological order.

Upvotes: 2

Related Questions