python-闹钟建议

标签 python time

我是 python 编程新手,我正在尝试应用我最近学到的概念。我正在阅读其他人的代码,找到了一个示例并对其进行了一些调整,如下所示。问题是当当前时间等于唤醒时间时,elif block 永远不会被触发。我已经在一个小的 if-else 语句中进行了测试,它工作正常,但从未在 while 循环 内触发。

请指教。

import time

import subprocess

wake_hour = int(raw_input("Wake hour: "))

wake_minute = int(raw_input("Wake minute: "))

wake_time = [wake_hour, wake_minute]

current_hour = int(time.strftime("%H"))

current_minute = int(time.strftime("%M"))

current_time = [current_hour, current_minute]

while True:

     if current_time != wake_time:
        current_hour = int(time.strftime("%H"))
        current_minute = int(time.strftime("%M"))
        print current_hour, current_minute
        print wake_hour, wake_minute

     elif current_time == wake_time:
        audio_file = "/Users/dcasteli/Documents/emergency018.mp3"
        play = subprocess.call(["afplay", audio_file])
        print "Wake up!"

最佳答案

您仅更新 current_hourcurrent_month,但在 elif 语句中您正在比较 current_time ==wake_timecurrent_time 值永远不会更新,并且无论用户输入什么,都将永远保留。在第一个 if 语句中,您应该更新 current_time 以便条件有意义。

关于python-闹钟建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46957536/

相关文章:

linux - Windows 时区 : Etc/UTC+05:30 (UTC, +0000) 和 UTC+05:30 之间有什么区别

Python Pandas Dataframe 日期时间列分隔函数

python - 有没有办法在 Python 的多行字符串中使用变量?

python - 将重复项添加为列并删除其他行

python - 显示运行 python 脚本的进度条

c - 获取c和windows中进程组的用户时间

python - 从大量字典创建字典的最快方法

python - 如何在字符串中的两个单词之间添加空格,以便总字符串长度具有特定值

java - 将 Joda-Time DateTime 格式化为字符串

macos - 时间命令的非法选项