Health Manager using Python

Health Manager using Python

Lets discuss how to make a Health Management System in Python using File Handing.

ยท

3 min read

What we are building?

The task is to build the health management system using file handling in Python that helps to keep track of all the diets and exercises a user has taken (in the form of files). We will give access to 3 user (can add more users) and each user will have their own data set in the respective file.

Features that are implemented in the program are :

  1. Add Exercise.
  2. Add Diet
  3. View Diet.
  4. View Exercise.
  5. Exit.

How to approach?

We will provide all the functions will be provided under if-else ladder.

  1. First we will print the name of the Management System and after that we will run a while loop that can be terminated using exit.
  2. We will give 3 options: Add data, View data and Exit. We will receive the user input and run the respective if condition.
  3. In both add and view data, user-name will be asked and then respective inner if-else ladder will be executed.
  4. For each user, we will create separate files for exercises and diet. Files will be created only once and for the next time, more data can be added to same file (using append option of file handling).

NOTE: We also use datetime to get the time and date of the entries. We will define a function and inside that function, we will import datetime module.

Below is the implementation of the above approach:

print("\t\t\t\t\t\t ~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("\t\t\t\t\t\t ||   THE HEALTH MANAGER  ||")
print("\t\t\t\t\t\t ~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("\tHi! Save your DIET & EXERCISE here.\n")

while(1):
    answer = int(input("Enter  1:View Saved Data\t\t\t2:Add Data\t\t\t3:EXIT\n"))

    if answer ==3:
        exit(0)

    name = int(input("Press 1 for Sushant \t 2 for Aman \t 3 for Aditya\n"))
    type = int(input("Press 1 for EXERCISE \t 2 for DIET\n"))

    def getdate():
        '''To get the current date and time at time of entry'''
        import datetime
        return (str(datetime.datetime.now()))

    #READING FROM THE FILE
    if answer == 1:
        # If the user is SUSHANT
        if name == 1:
            if type == 1:
                try:
                    f = open("sushant_exer.txt", "rt")
                    print("\nDetails :")
                    print(f.read())
                    f.close()
                except:
                    print("Details not found")

            elif type == 2:
                try:
                    f = open("sushant_diet.txt", "rt")
                    print("\nDetails :")
                    print(f.read())
                    f.close()
                except:
                    print("Details not found")

            else:
                print("\nWRONG INPUT!")


        # If the user is AMAN
        elif name == 2:
            if type == 1:
                try:
                    f = open("aman_exer.txt", "rt")
                    print("\nDetails :")
                    print(f.read())
                    f.close()
                except:
                    print("Details not found")

            elif type == 2:
                try:
                    f = open("aman_diet.txt", "rt")
                    print("\nDetails :")
                    print(f.read())
                    f.close()
                except:
                    print("Details not found")

            else:
                print("\nWRONG INPUT!")


        # If the user is ADITYA
        elif name == 3:
            if type == 1:
                try:
                    f = open("aditya_exer.txt", "rt")
                    print("\nDetails :")
                    print(f.read())
                    f.close()
                except:
                    print("Details not found")

            elif type == 2:
                try:
                    f = open("aditya_diet.txt", "rt")
                    print("\nDetails :")
                    print(f.read())
                    f.close()
                except:
                    print("Details not found")

            else:
                print("\nWRONG INPUT!")


    #WRITING TO THE FILE
    if answer == 2:
        # If the user is SUSHANT
        if name == 1:
            if type == 1:
                f = open("sushant_exer.txt","at")
                f.write("[ "+getdate()+" ] ")
                f.write(input("Enter exercise you have done : "))
                f.write("\n")
                f.close()

            elif type == 2:
                f = open("sushant_diet.txt","at")
                f.write("[ "+getdate()+" ] ")
                f.write(input("Enter diet you have taken : "))
                f.write("\n")
                f.close()

            else:
                print("\nWRONG INPUT!")


        # If the user is AMAN
        elif name == 2:
            if type == 1:
                f = open("aman_exer.txt","at")
                f.write("[ "+getdate()+" ] ")
                f.write(input("Enter exercise you have done : "))
                f.write("\n")
                f.close()

            elif type == 2:
                f = open("aman_diet.txt","at")
                f.write("[ "+getdate()+" ] ")
                f.write(input("Enter diet you have taken : "))
                f.write("\n")
                f.close()

            else:
                print("\nWRONG INPUT!")


        # If the user is ADITYA
        elif name == 3:
            if type == 1:
                f = open("aditya_exer.txt", "at")
                f.write("[ "+getdate()+" ] ")
                f.write(input("Enter exercise you have done : "))
                f.write("\n")
                f.close()

            elif type == 2:
                f = open("aditya_diet.txt", "at")
                f.write("[ "+getdate()+" ] ")
                f.write(input("Enter diet you have taken : "))
                f.write("\n")
                f.close()

            else:
                print("\nWRONG INPUT!")

Sample Input/Output:

image.png

image.png

ย