我需要写一个python程序,让我在特定的时间设置提醒,例如“记得下午2点拿出垃圾箱”,但我只能在一定的时间长度内设置提醒,而不是给定的时间。我还需要能够设置多个提醒多次。如有任何帮助,我们将不胜感激:)
发布于 2021-11-16 21:43:53
这看起来像是家庭作业,所以你需要自己写代码。
,
如果使用适当的数据结构,如heapq或PriorityQueue,您可能会发现步骤2更容易。但是,如果警报的数量很少,那么列表就可以了。
发布于 2021-12-11 06:11:55
下面每秒钟检查一次新的提醒,尽管在阅读Frank的答案后,这将是一个更好的解决方案,最好的解决方案是根本不使用Python,并让操作系统通过创建cron作业或在Windows上创建计划任务来管理这一点。
reminders = [
# Put all of your reminders here
('2021-11-16 02:44:00', 'Take out the garbage'),
('2021-11-17 04:22:00', 'Another reminder')
]
from datetime import datetime
import time
# For performance reasons it's best to perform whatever computations we can before we go into our infinite loop
# In this case let's calculate all of the timestamps
reminders2 = {datetime.fromisoformat(reminder[0]).timestamp(): reminder[1] for reminder in reminders}
while True:
now = time.time()
for timestamp, reminder_msg in reminders2.items():
if timestamp < now:
print(reminder_msg)
del reminders2[timestamp]
# we're not in any hurry, instead of worrying about the consequences of deleting something from the same dictionary we are iterating over
# we can just break and wait for the next go around of the while loop to finish checking the remaining reminders
break
time.sleep(1) # in secondshttps://stackoverflow.com/questions/69996306
复制相似问题