运算符在while-loop-Python中不工作

运算符在while-loop-Python中不工作,python,python-3.x,Python,Python 3.x,这个python脚本应该在所需的时间关闭计算机,但问题是,在第9行,如果“小时”等于当前小时,则程序不会查看分钟数。我应该添加或更改什么? 谢谢 正如user2357112所说,您在启动程序时设置了hour和minute,但不再更新 这应该起作用: while hour != 23 and minute != 28: print("not yet!") time.sleep(50) hour = dt.datetime.now().hour minute = dt

这个python脚本应该在所需的时间关闭计算机,但问题是,在第9行,如果“小时”等于当前小时,则程序不会查看分钟数。我应该添加或更改什么? 谢谢


正如
user2357112
所说,您在启动程序时设置了
hour
minute
,但不再更新

这应该起作用:

while hour != 23 and minute != 28:
    print("not yet!")
    time.sleep(50)
    hour = dt.datetime.now().hour
    minute = dt.datetime.now().minute

print("Your computer is about to get shutdowned")
subprocess.call(["shutdown", "/s"])

如果是关机时间,你不需要每2秒检查一次。一分钟两次就足够了。

你应该在循环中得到小时和分钟,当你启动脚本时,每个循环只得到一个值,并且值永远不会更新

尝试以下方法:

while hour != 23 and minute != 28:
    hour = dt.datetime.now().hour
    minute = dt.datetime.now().minute
    time.sleep(2)
    print("not yet!")

将“和”改为“或”。此外,您还必须更新循环中的小时和分钟。

实际上,您需要计算下一个23:28的到期日:

import datetime
import time

now = datetime.datetime.now()
due_date = now.replace(hour=23, minute=28)
if due_date < now:
    due_date += datetime.timedelta(days=1)
导入日期时间
导入时间
now=datetime.datetime.now()
截止日期=现在。更换(小时=23,分钟=28)
如果到期日<现在:
到期日+=datetime.timedelta(天数=1)
然后,您可以进行倒计时:

print("Your computer is about to get shutdowned...")
while datetime.datetime.now() < due_date:
    time.sleep(2)
    duration = due_date - datetime.datetime.now()
    print("... in {:d} seconds".format(int(duration.seconds)))
print(“您的计算机即将关闭…”)
while datetime.datetime.now()到期日:
时间。睡眠(2)
持续时间=到期日-datetime.datetime.now()
打印(“…以{:d}秒为单位”。格式(int(duration.seconds)))

您从不更新
hour
minute
。您希望
而不是(hour==23和minute==28)
作为您的条件,并且您还需要更新循环中的这些变量。非常感谢!现在一切正常:)
print("Your computer is about to get shutdowned...")
while datetime.datetime.now() < due_date:
    time.sleep(2)
    duration = due_date - datetime.datetime.now()
    print("... in {:d} seconds".format(int(duration.seconds)))