我想设置时间以灵活的时间通知用户,我想我想input
。
我可以在特定时间获得期望的输出,但是当我输入期望的时间时,它不会通过程序。再次要求input
时间:
我需要打印时间并通知并继续执行程序:
20:24:00
>
>>> from win10toast import ToastNotifier
>>> import time
>>> def run_app():
>>> show_help()
>>> while True:
>>> #set time to notify for shopping
>>> notification_time = time.strftime("%H:%M:%S")
>>> if notification_time == input("Please enter a specific time:\n"):
>>> print(notification_time)
>>>
>>> break
>>> else:
>>> pass
>>> #organise the notification
>>> notification1= ToastNotifier()
>>> notification1.show_toast("Alarm","It is time to shop ")
现在的结果是:
Please enter a specific time:
20:24:00
Please enter a specific time:
...
答案 0 :(得分:0)
您的输入回路逻辑错误。它与您要说的完全不符:
notification_time = time.strftime("%H:%M:%S")
# This stores the current time, mis-named "notification_time"
if notification_time == input("Please enter a specific time:\n"):
# This requires the user to enter a time in the required format.
# If that doesn't match the current time at the previous instruction,
# you ignore the input and repeat the process.
最后,如果用户确实设法在当前时间输入当前时间,则将其作为通知时间。用户无法输入将来的时间。
相反,您需要接受用户的通知时间,然后检查该格式的有效性。最简单的方法通常是接受输入字符串,然后使用try/except
块将其转换为时间。如果失败,则循环返回以再次尝试输入。
答案 1 :(得分:0)
我试图将您的想法变成功能代码:
from datetime import datetime
import time
from win10toast import ToastNotifier
toaster = ToastNotifier()
# Validate if the format of the time that the user submitted is correct
def isTimeCorrect(input):
try:
time.strptime(input, '%H:%M:%S')
return True
except ValueError:
return print('time is wrong')
# Request the user to type the alarm time
alarm = input('Type your alarm time in this format HH:MM:SS\n')
# Get the current time
now = datetime.now()
# Format the current time
current_time = now.strftime("%H:%M:%S")
# Loop and check the current time until it's the same as the alarm
while (isTimeCorrect(alarm)):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
# Check if the current time is the same as the alarm
if current_time == alarm:
# throw the notification
toaster.show_toast("Example","Notifcation Text",icon_path=None,duration=5,threaded=True)
break