1

Hi I'm new to coding and need some help with an alarm. When running this I am having problems with the output in two areas. The first is ":" between the alarma and alarmb, I am trying to get the format to show (3:29 PM) and it is showing (3 : 29 PM). The second issue is with alarmb anything entered under 10 for input will show without the 0 such as 3 : 1 PM rather than 3 : 01 PM.

#Alarm clock with sound

from playsound import playsound
import time
import datetime
import os
os. system('cls')


a = "Good Morning!"
t = datetime.datetime.now()

alarma = int(input("What Hour do you want me to wake you up at? "))
alarmb = int(input("What Minute do you want me to wake you up at? "))
alarmc = str(input("AM or PM "))

os. system('cls')

print("Alarm set for",alarma,":",alarmb, alarmc)
   os. system('cls')

if(alarmc == "PM"):
     alarma = alarma + 12

while True:
    if(alarma == datetime.datetime.now().hour and
    alarmb == datetime.datetime.now().minute) :
        print(a)
        print(t.strftime("The time is now %I:%M %p"))
        print(t.strftime("It is currently %A, %B %d, %Y"))
        playsound("WakeUp.mp3")
        break
Jordan
  • 11
  • 1
  • Welcome to stackoverflow, @Jordan. Consider using [timer](https://stackoverflow.com/questions/11523918/python-start-a-function-at-given-time) instead of a while loop when you are checking for time. [Busy waiting](https://en.wikipedia.org/wiki/Busy_waiting) is never a good option. Thanks! – tornikeo Sep 07 '20 at 03:44

1 Answers1

2

This is because the different parameters given to method print are join by a space by default. You'd better use f-string, that easier for formatting, and use 02d to get a 0-leading

print(f"Alarm set for {alarma:02d}:{alarmb:02d} {alarmc}")
azro
  • 53,056
  • 7
  • 34
  • 70