Create Health Clock for programmers using Python
In this article, we will be creating a Python script that takes care of the health of a person especially a programmer. It will remind the user to drink water, do an eye exercise, and do a physical exercise at different specified time intervals. It will also keep all the log of time at which the exercises are done, water is drunk as well as the no of exercises and quantity of water taken. At the end of the program, whole stats will be shown along with time stamps.
Approach:
- We will import mixer from pygame module to play our sound, time from time module to set the time interval, and sleep from time module to freeze the program for a respective time interval.
- We will define two functions- getdate() that will return the current date and time when the function is called, and musicloop() that will play the given file.
- After that, we will initialize the water, eye exercise, and physical exercise variable with the current time. We are doing this so that we can play the sound at the equal desired time interval.
- Now, we will be running an infinite while loop that will contain three if conditions (one for each i.e. water, eye exercise, and physical exercise).
- In each of the if conditions, we will be doing the same thing i.e. calling the musicloop() function to play music, asking the user to do a respective thing to stop music, and noting the time (of completion of exercise) in a file so that it can be shown at last.
- We will also increment no of exercises and value of water drank so that we can print it at the end of the program.
- At last, we are using file handling to display all the above-mentioned data.
Below is the Implementation.
Python3
# import required modulesfrom pygame import mixerfrom time import timefrom time import sleep def getdate(): # To get the current date and time # at time of entry import datetime return (str(datetime.datetime.now())) def musicloop(stopper): mixer.init() mixer.music.load("music.mp3") # playing the music provided i.e. music.mp3 mixer.music.play() while True: x = input( "Please type STOP to stop the alarm or EXIT to stop the program : ") # music termination condition. if (x.upper() == stopper): print("\nGreat! Get back to work:)\n") mixer.music.stop() return True break # program termination condition. elif (x.upper() == "EXIT"): return False # variables initialized with 0 for counting total # number of exercises and water drank in a daytotal_water = 0total_physical_exercises = 0total_eye_exercises = 0 if __name__ == '__main__': print("\n\t\t\t\tHey Programmer! This is your Health-Alarm-Clock\n") time_phy = time() time_drink = time() time_eyes = time() eyes_delay = 10 drink_delay = 20 phy_delay = 35 while(True): # Drink water condition. if (time() - time_drink > drink_delay): print("Hey! Please drink some water (at least 200 ml).") # Checking the user input so that music # can be stopped. if(musicloop("STOP")): pass else: break # reinitializing the variable time_drink = time() # incrementing the value total_water += 200 # opening the file and putting the data # into that file f = open("drank.txt", "at") f.write("[ " + getdate() + " ] \n") f.close() # Eye exercise condition. if (time() - time_eyes > eyes_delay): print("Hey! Please do an Eye Exercise.") if (musicloop("STOP")): pass else: break time_eyes = time() total_eye_exercises += 1 f = open("eye.txt", "at") f.write("[ " + getdate() + " ] \n") f.close() # Eye exercise condition. if (time() - time_phy > phy_delay): print("Hey! Please do a Physical Exercise.") if (musicloop("STOP")): pass else: break time_phy = time() total_physical_exercises += 1 f = open("phy_exer.txt", "at") f.write("[ " + getdate() + " ] \n") f.close() print() print(f"Total water taken today : {total_water}.") try: f = open("drank.txt", "rt") print("\nDetails :") print(f.read()) f.close() except: print("Details not found!") print(f"Total eye exercise done today : {total_eye_exercises}.") try: f = open("eye.txt", "rt") print("\nDetails :") print(f.read()) f.close() except: print("Details not found!") print(f"Total physical exercises done today : {total_physical_exercises}.") try: f = open("phy_exer.txt", "rt") print("\nDetails :") print(f.read()) f.close() except: print("Details not found!") sleep(5) |
Output:


Video Demonstration:



