使用python跟踪并不断更新离校天数

使用python跟踪并不断更新离校天数,python,countdown,Python,Countdown,我正在尝试创建一个程序,询问那天是否有学校,如果有,从总数中减去(1-22-18剩下86天)。它可以工作,但程序在一次减法后结束,所以我的问题是,有没有办法让它继续运行并自我更新,或者在24小时内再次询问用户(不知道如何) Python 3.4.4 视窗10 import time localtime = time.asctime(time.localtime(time.time())) day = localtime[0:3] check = 0 daysLeft = 87 #As of 1

我正在尝试创建一个程序,询问那天是否有学校,如果有,从总数中减去(1-22-18剩下86天)。它可以工作,但程序在一次减法后结束,所以我的问题是,有没有办法让它继续运行并自我更新,或者在24小时内再次询问用户(不知道如何)

Python 3.4.4 视窗10

import time

localtime = time.asctime(time.localtime(time.time()))
day = localtime[0:3]
check = 0
daysLeft = 87 #As of 1-22-18
daysOfTheWeek = ["Mon", "Tue", "Wed", "Thu", "Fri"]
yesPossibilities = ["yes", "y", "yeah"]
print ("Did you have school today?")
schoolToday = input().lower()

if schoolToday in yesPossibilities:
    if day in daysOfTheWeek:
        daysLeft -= 1

print ("There are", daysLeft, "days of school left!")

你需要一个无限循环和一个睡眠计时器

import time
time.sleep(86400) #this will make the code sleep for 1 day = 86400 seconds
接下来,将睡眠放入无限循环

while True:
    #get input
    if input meets condition:
        reduce day count by 1
        print number of days left
        time.sleep(86400)
    if days left meets some threshold:
        print "school over"
        break

我认为您真正想做的是在每次运行脚本时保存结果(例如:如果您今天运行它,它会告诉您还有86天,如果您明天运行它,它会告诉您还有85天,等等)。您可能不想永远运行脚本,因为如果关闭计算机,脚本将终止,这意味着您将丢失所有结果。我将以以下方式将输出保存到文本文件:

print("There are" daysLeft, "days of school left!")
with open("EnterNameOfFileHere.txt",'w') as f:
    print(daysLeft,file=f)
check = 0
with open("EnterNameOfFileHere.txt") as f:
    daysLeft = int(f.readline().strip())
daysOfTheWeek = ....
这将把daysLeft变量保存在文本文件中,您可以在程序开始时通过以下方式访问该文件:

print("There are" daysLeft, "days of school left!")
with open("EnterNameOfFileHere.txt",'w') as f:
    print(daysLeft,file=f)
check = 0
with open("EnterNameOfFileHere.txt") as f:
    daysLeft = int(f.readline().strip())
daysOfTheWeek = ....

总之,实现此功能将允许您在每次运行脚本时保存结果,以便下次运行脚本时可以从该值开始。

欢迎使用StackOverflow。请使用本网站提供的格式将代码及其输出格式化为问题中的文本(而不是链接到图像)。