Search code examples
pythoncountdown

Using python to track and constantly update days left of school


I'm trying to create a program that asks if there was school that day and if so, subtracts that from the total, (86 days left as of 1-22-18). It works, but the program ends after one subtraction, so my question is, is there any way for it to continue running and update itself, or maybe ask the user again in 24 hours (no clue how)?

Python 3.4.4 Windows 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!")

Solution

  • I think what you're really trying to do is save the results each time you run your script (Ex: If you run it today, it tells you there are 86 days left, if you run it tomorrow, it tells you there are 85 days left, etc). You probably don't want to run the script forever because if you turn your computer off, the script is terminated, which means you'll lose all of your results. I would save the output to a text file in the following manner:

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

    This will save the daysLeft variable in a text file, which you can access at the start of your program in the following manner:

    check = 0
    with open("EnterNameOfFileHere.txt") as f:
        daysLeft = int(f.readline().strip())
    daysOfTheWeek = ....
    

    In summary, implementing this will allow you to save your results each time you run your script so that you can start from that value the next time you run the script.