Course Content

Exercise 7: Healthy Programmer

Exercise#7 Healthy Programmer

Assume that a programmer works at the office from 9-5 pm. We have to take care of his health and remind him three things,

  • To drink a total of 3.5-liter water after some time interval between 9-5 pm.
  • To do eye exercise after every 30 minutes.
  • To perform physical activity after every 45 minutes.

Instructions:

The task is to create a program that plays mp3 audio until the programmer enters the input which implies that he has done the task.

  • For Water, the user should enter “Drank”
  • For Eye Exercise, the user should enter “EyDone”
  • For Physical Exercise, the user should enter “ExDone”

After the user entered the input, a file should be created for every task separately, which contains the details about the time when the user performed a certain task.

Challenge:

  • You will have to manage the clashes between the reminders. Such that no two reminders play at the same time.
  • Use pygame module to play audio.

If you are following the lectures regularly, then I'm sure you can complete the task in no time.

Your participation is appreciated. Keep supporting and stay up to date with codewithharry

Code file as described in the video

#Healthy Programmer
# 9am - 5pm
# Water - water.mp3 (3.5 litres) - Drank - log
# Eyes - eyes.mp3 - every 30 min - EyDone - log
# Physical activity - physical.mp3 every - 45 min - ExDone - log
#
# Rules
# Pygame module to play audio
  

Comments(3)

oyanhr 3 months, 1 week ago
Thanks Harry vai. I have successfully completed the 50th video. Sometimes things seem very confusing. But still I watch them and repeat them the next day. Then those become very clear.
charlie 3 months ago
Sir, please give mp3s.
charlie 3 months ago
Username="User"
import time
# import random
from pygame import mixer
def gettime():
    import time
    localtime = time.asctime(time.localtime(time.time()))
    return localtime
def log(x):
    z=open("Health.txt","a")
    z.write("At " )
    z.write(str(gettime()))
    z.write(f":=> {x} \n ")
    z.close()
def music(announcement,mp3,vol,stopword,logname):
    """ >>>>>>This function takes 05 parameters as described below:
        1)announcement (str) - This takes what is to be announced while starting music
        2)mp3 (mp3 filename) - This takes name of the music file.
        3)vol (int) - This takes what should be the volume of the music to be played
        4)stopword (str) - This takes what should be entered to stop the music
        5)logname(str) - Type of activity by which your log is saved"""
    print(announcement)
    mixer.init()
    mixer.music.load(mp3)
    mixer.music.set_volume(vol)
    mixer.music.play()
    print(f"Enter {stopword} to Stop the music")
    while (True):
        z = input()
        if z == stopword:
            mixer.music.stop()
            log(f" {Username} {logname}")
            break
        else:
            print(f"Invalid input ! please Enter {stopword} to Stop music. ")
            continue
watertime=time.time()
eyetime=time.time()
exetime=time.time()
waterzone=27*60
eyezone=30*60
exezone=45*60
while(True):
    if time.localtime().tm_hour ==9 and time.localtime().tm_min==00 and time.localtime().tm_sec==00 :
        while(True):
            if time.time() - watertime >= waterzone:
                music("Drink water", "water.mp3", 0.7, "Drank", "Drank water")
                print("Done!")
                watertime = time.time()
                continue
            if time.time() - eyetime >= eyezone:
                music("Eye Exercise", "eyes.mp3", 0.7, "EyDone", "Done eye Exercise")
                print("Done!")
                eyetime = time.time()
                continue
            if time.time() - exetime >= exezone:
                music("Exercise time", "physical.mp3", 0.7, "ExDone", "Done Physical Exercise")
                print("Done!")
                exetime = time.time()
                continue
            if time.localtime().tm_hour == 17 and time.localtime().tm_min == 00 and time.localtime().tm_sec == 00:
                break
    if time.localtime().tm_hour == 17 and time.localtime().tm_min == 00 and time.localtime().tm_sec == 00:
            print("Exiting Program")
            time.sleep(5)
            break

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here