用Python创建警报,输入和输出问题

时间:2020-09-06 21:12:47

标签: python syntax alarm

您好,我是编码的新手,并且需要一些有关警报的帮助。运行此程序时,我在两个区域中遇到输出问题。第一个是alarma和alarmb之间的“:”,我试图获取要显示的格式(下午3:29),并且正在显示(3:29 PM)。第二个问题是与Alarmb有关的问题,在10以下输入的任何内容都将不显示0,例如3:1 PM而不是3:01 PM。

#带声音的闹钟

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

1 个答案:

答案 0 :(得分:2)

这是因为默认情况下,给方法print赋予的不同参数由空格连接。您最好使用f-string(这样更易于格式化),并使用02d获得0开头

print(f"Alarm set for {alarma:02d}:{alarmb:02d} {alarmc}")