Exercise 7: Solution & First Solver

 Healthy Programmer. A program we coders definitely require. The problem statement to the exercise is:

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.

from pygame import mixer meaning:

In order to play music/audio files in pygame , pygame.mixer is used (pygame module for loading and playing sounds). This module contains classes for loading Sound objects and controlling playback.

from datetime import datetime python meaning:
In Python, date and time are not a data type of their own, but a module named datetime can be imported to work with the date as well as time. ... datetime – Its a combination of date and time along with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.

from time import time meaning:

As the name suggests Python time module allows to work with time in Python. It allows functionality like getting the current time, pausing the Program from executing, etc. So before starting with this module we need to import it.

What is mixer init ()?:
pygame.mixer.init. — initialize the mixer module.
mixer module helps control the music used in pygame programs.

Python | Playing audio file in Pygame:
  1. Starting the mixer mixer.init()
  2. Loading the song. mixer.music.load("song.mp3")
  3. Setting the volume. mixer.music.set_volume(0.7)
  4. Start playing the song. mixer.music.play()

if __name__ == “__main__” :

 simple words, by using __name__, we can check whether our module is being imported or run directly.

If we run it in the same module that it is created in, then it will print “main” onto the screen; otherwise, if it is being used elsewhere, then it will print the name of its module or file it is created in.

main is a point of the program from where the program starts its execution. Every program has its own main function. The main function can only be executed when it is being run in the same program. If the file is being imported, then it is no longer the main function because the file that is importing it has its own “main” function.

Code as described/written in the video


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

from pygame import mixer
from datetime import datetime
from time import time

def musiconloop(file, stopper):
    mixer.init()
    mixer.music.load(file)
    mixer.music.play()
    while True:
        input_of_user = input()
        if input_of_user == stopper:
            mixer.music.stop()
            break

def log_now(msg):
    with open("mylogs.txt", "a") as f:
        f.write(f"{msg} {datetime.now()}\n")

if __name__ == '__main__':
    # musiconloop("water.mp3", "stop")
    init_water = time()
    init_eyes = time()
    init_exercise = time()
    watersecs = 40*60
    exsecs = 30*60
    eyessecs = 45*60

    while True:
        if time() - init_water > watersecs:
            print("Water Drinking time. Enter 'drank' to stop the alarm.")
            musiconloop('water.mp3', 'drank')
            init_water = time()
            log_now("Drank Water at")

        if time() - init_eyes >eyessecs:
            print("Eye exercise time. Enter 'doneeyes' to stop the alarm.")
            musiconloop('eyes.mp3', 'doneeyes')
            init_eyes = time()
            log_now("Eyes Relaxed at")

        if time() - init_exercise > exsecs:
            print("Physical Activity Time. Enter 'donephy' to stop the alarm.")
            musiconloop('physical.mp3', 'donephy')
            init_exercise = time()
            log_now("Physical Activity done at")



Comments

Popular posts from this blog

HOW IMPORT WORKS IN PYTHON?

Exercise 5: Health Management System

*args and **kwargs In Python