试图用Python制作闹钟,但没有成功



不知道我被困在哪里了。对我来说似乎很好。也许循环部分不能正常工作?如果有什么问题,请告诉我。

import datetime
import time

year = (datetime.datetime.today().strftime("%Y"))
month = (datetime.datetime.today().strftime("%m"))
day = (datetime.datetime.today().strftime("%d"))
hour = (datetime.datetime.today().strftime("%H"))
minute = (datetime.datetime.today().strftime("%M"))
second = (datetime.datetime.today().strftime("%S"))
setyear = int(input("Year"))
setmonth = int(input("Month"))
setday = int(input("Day"))
sethour = int(input("Hour"))
setminute = int(input("Minute"))
setsecond = int(input("Second"))
while True:
time.sleep(1)
year = (datetime.datetime.today().strftime("%Y"))
month = (datetime.datetime.today().strftime("%m"))
day = (datetime.datetime.today().strftime("%d"))
hour = (datetime.datetime.today().strftime("%H"))
minute = (datetime.datetime.today().strftime("%M"))
second = (datetime.datetime.today().strftime("%S"))
if year == setyear and month == setmonth and day == setday and hour == sethour and minute == setminute and second == setsecond:
print("Alarm!")  
break
else:
print("NO")

我相信,因为类型不匹配,因此值的解释不同。

例如:

输入秒";0";将与返回的秒数"0"不匹配;00〃;,我删除了你的int((转换开始时,它现在工作。你只需要输入正确的数字:

import time
import datetime

year = (datetime.datetime.today().strftime("%Y"))
month = (datetime.datetime.today().strftime("%m"))
day = (datetime.datetime.today().strftime("%d"))
hour = (datetime.datetime.today().strftime("%H"))
minute = (datetime.datetime.today().strftime("%M"))
second = (datetime.datetime.today().strftime("%S"))

setyear = (input("Year"))
setmonth = (input("Month"))
setday = (input("Day"))
sethour = (input("Hour"))
setminute = (input("Minute"))
setsecond = (input("Second"))
while True:
print(year,'-',month,'-',day,' ',hour,':',minute,':',second)
time.sleep(1)
year = (datetime.datetime.today().strftime("%Y"))
month = (datetime.datetime.today().strftime("%m"))
day = (datetime.datetime.today().strftime("%d"))
hour = (datetime.datetime.today().strftime("%H"))
minute = (datetime.datetime.today().strftime("%M"))
second = (datetime.datetime.today().strftime("%S"))
if year == setyear and month == setmonth and day == setday and hour == sethour and minute == setminute and second == setsecond:
print("Alarm!")  
break
else:
print("NO")

输出:

Year2020
Month11
Day12
Hour14
Minute12
Second00
2020 - 11 - 12   14 : 10 : 56
NO
2020 - 11 - 12   14 : 11 : 08
NO
....
2020 - 11 - 12   14 : 11 : 59
Alarm!

最新更新