Course Content

Exercise 5: Solution And First Solver

This tutorial contains the solution of Exercise-5 (Health Management System). We used simple files and some fundamental concepts of Python to accomplish this Exercise. Your answer or code may vary, but it should give the same output or result, so don't get bothered about it. 

Instructions:

  • Create a food log file for each client
  • Create an exercise log file for each client.
  • Ask the user whether they want to log or retrieve client data.
  • Write a function that takes the user input of the client's name. After the client's name is entered, A message should display "What you want to log. Diet or Exercise"
  • Use function

 

def getdate():
           import datetime
           return datetime.datetime.now()
  • The purpose of this function is to give time with every record of food or exercise added in the file.
  • Write a function to retrieve exercise or food file records for any client.

Code file as described in the video

# Health Management System
# 3 clients - Harry, Rohan and Hammad

def getdate():
    import datetime
    return datetime.datetime.now()

# Total 6 files
# write a function that when executed takes as input client name
# One more function to retrieve exercise or food for any client


#bhai ye rha program
import datetime
def gettime():
    return datetime.datetime.now()
def take(k):
    if k==1:
        c=int(input("enter 1 for excersise and 2 for food"))
        if(c==1):
            value=input("type here\n")
            with open("harry-ex.txt","a") as op:
                op.write(str([str(gettime())])+": "+value+"\n")
            print("successfully written")
        elif(c==2):
            value = input("type here\n")
            with open("harry-food.txt", "a") as op:
                op.write(str([str(gettime())]) + ": " + value + "\n")
            print("successfully written")
    elif(k==2):
        c = int(input("enter 1 for excersise and 2 for food"))
        if (c == 1):
            value = input("type here\n")
            with open("rohan-ex.txt", "a") as op:
                op.write(str([str(gettime())]) + ": " + value + "\n")
            print("successfully written")
        elif (c == 2):
            value = input("type here\n")
            with open("rohan-food.txt", "a") as op:
                op.write(str([str(gettime())]) + ": " + value + "\n")
            print("successfully written")
    elif(k==3):
        c = int(input("enter 1 for excersise and 2 for food"))
        if (c == 1):
            value = input("type here\n")
            with open("hammad-ex.txt", "a") as op:
                op.write(str([str(gettime())]) + ": " + value + "\n")
            print("successfully written")
        elif (c == 2):
            value = input("type here\n")
            with open("hammad-food.txt", "a") as op:
                op.write(str([str(gettime())]) + ": " + value + "\n")
            print("successfully written")
    else:
        print("plz enter valid input (1(harry),2(rohan),3(hammad)")
def retrieve(k):
    if k==1:
        c=int(input("enter 1 for excersise and 2 for food"))
        if(c==1):
            with open("harry-ex.txt") as op:
                for i in op:
                    print(i,end="")
        elif(c==2):
            with open("harry-food.txt") as op:
                for i in op:
                    print(i, end="")
    elif(k==2):
        c = int(input("enter 1 for excersise and 2 for food"))
        if (c == 1):
            with open("rohan-ex.txt") as op:
                for i in op:
                    print(i, end="")
        elif (c == 2):
            with open("rohan-food.txt") as op:
                for i in op:
                    print(i, end="")
    elif(k==3):
        c = int(input("enter 1 for excersise and 2 for food"))
        if (c == 1):
            with open("hammad-ex.txt") as op:
                for i in op:
                    print(i, end="")
        elif (c == 2):
            with open("hammad-food.txt") as op:
                for i in op:
                    print(i, end="")
    else:
        print("plz enter valid input (harry,rohan,hammad)")
print("health management system: ")
a=int(input("Press 1 for log the value and 2 for retrieve "))

if a==1:
    b = int(input("Press 1 for harry 2 for rohan 3 for hammad "))
    take(b)
else:
    b = int(input("Press 1 for harry 2 for rohan 3 for hammad "))
    retrieve(b)
  

Comments(4)

avhigupta 4 months, 1 week ago
sir the code you have provided here for retrive it works for every no. expect 1 so it need modification at end where 
 elif a== 2:
    b = int(input("Press 1 for harry 2 for rohan 3 for hammad "))
    retrieve(b)
else:
    print("invalid number")
avhigupta 4 months, 1 week ago
sir and plz what retrive mean
sanam007 4 months, 1 week ago


plz chk krlo  bhai

print("HEALTH MANAGEMENT SYSTEM")
user = int(input(" 1 for log ; 2 for retrive: \n "))
name = int(input(" ENTER CLIENT'S NAME: 1 FOE MARRY 2 FOR ROHAN 3 FOR HAMMAD: \n "))
routine = int(input(" 1 for diet ; 2 for workout: \n "))

def getdate():
    import datetime
    return datetime.datetime.now()
q = getdate()
print(q, end="\n")

#log

if user == 1:
    def diet1():
        if name == 1 and routine == 1:
            with open("marrydeit.txt", "a") as a:
                b = a.write(input("ADD ITEMS IN YOUR DIET LIST "))
                print(b)
                print("LOGGED SUCESSFULLY! ")
        elif name == 2 and routine == 1:
            with open("rohandiet.txt", "a") as e:
                f = e.write(input("ADD ITEMS IN YOUR DIET LIST "))
                print(f)
                print("LOGGED SUCESSFULLY! ")
        elif name == 3 and routine == 1:
            with open("hammaddiet.txt", "a") as i:
                j = i.write(input("ADD ITEMS IN YOUR DIET LIST "))
                print(j)
                print("LOGGED SUCESSFULLY! ")

    def workout1():
        if name == 1 and routine == 2:
            with open("marryworkout.txt", "a") as c:
                d = c.write(input("ADD ITEMS IN YOUR WORKOUT LIST "))
                print(d)
                print("LOGGED SUCESSFULLY! ")
        elif name == 2 and routine == 2:
            with open("rohanworkout.txt", "a") as g:
                h = g.write(input("ADD ITEMS IN YOUR WORKOUT LIST "))
                print(h)
                print("LOGGED SUCESSFULLY! ")
        elif name == 3 and routine == 2:
            with open("hammadworkout.txt", "a") as k:
                l = k.write(input("ADD ITEMS IN YOUR WORKOUT LIST "))
                print(l)
                print("LOGGED SUCESSFULLY! ")
    diet1()
    workout1()


#retrive

if user == 2:
    def diet():
        if name == 1 and routine == 1:
            with open("marrydeit.txt") as a:
                b = a.read()
                print(b)
        elif name == 2 and routine == 1:
            with open("rohandiet.txt") as e:
                f = e.read()
                print(f)
        elif name == 3 and routine == 1:
            with open("hammaddiet.txt") as i:
                j = i.read()
                print(j)

    def workout():
        if name == 1 and routine == 2:
            with open("marryworkout.txt") as c:
                d = c.read()
                print(d)
        elif name == 2 and routine == 2:
            with open("rohanworkout.txt") as g:
                h = g.read()
                print(h)
        elif name == 3 and routine == 2:
            with open("hammadworkout.txt") as k:
                l = k.read()
                print(l)
    diet()
    workout()
ashu1373 4 months ago
Please review my code:
TRied Contacting you evrywhere.. Hope my effort pays...
Thanks in advance...

# Health Management System
"""
Problem Statement:
3 clients: Ashish, Prasad, Manoj
Diet
Exercises they should perform
3 seperate files to log the food and 3 separate files to log the exercises = Total 6 files
write a function that when executed takes as input client's name (input), what do you want to log??

write one more funtion to retrieve exercise or food for any client
1. Log or retrieve
2. for whom
3. what to log exercise or food
4. message to show that successfully logged the data
"""


#------------------------------------------------------------------------------------------

# funtion to get the time:
def getdate():
    import datetime
    return datetime.datetime.now()

#------------------------------------------------------------------------------------------

def append1():
    f = open(file1, "a")
    f.write("[")
    a = str(getdate())
    f.write(a)
    f.write("]  ",)
    if cat == 1:
        x = input("Enter the Food Consumed: ")
    else:
        x = input("Enter the Exercises DONE: ")
    f.write(x)
    f.write("\n")
    f.close()
    print("You have Successfully Logged the Data in the file...Thank you..")

# #----------------------------------------------------------------------------------------

def ret1():
    f = open(file1, "r")
    print(f.read())

#----------------------------------LISTS for FILES-----------------------------------------

food = ["ashish_food.txt", "prasad_food.txt", "manoj_food.txt"]
exercise = ["ashish_exercise.txt", "prasad_exercise.txt", "manoj_exercise.txt"]

#--------------------------------MAIN PROGRAM----------------------------------------------
while(True):

    print("***********************************************")
    print("Hello.. Welcome to the HEALTH MANAGEMENT SYSTEM")
    print("***********************************************")

    print("\nWhat operation do you want to perform? ",)
    print("1. Log the information\n2. Retrieve the information")
    op = int(input("[Press 1/2]: "))

    if op > 2 or op <1:
        print("-------------------------------------------------------------")
        print("Wrong Entry...Please Enter the VALID number and TRY AGAIN..")
        print("-------------------------------------------------------------")
        continue
    else:
        while(True):
            print("\nFor whom do you want to perform the operation?")
            print("1. Ashish\n2. Prasad\n3. Manoj")
            name = int(input("[Press 1/2/3]: "))

            if name < 1 or name > 3:
                print("-------------------------------------------------------------")
                print("Wrong Entry...Please Enter the VALID number and TRY AGAIN..")
                print("-------------------------------------------------------------")
                continue
            else:
                while(True):
                    print("\nWhat do you want to log the information for? ")
                    print("1. Food\n2. Exercise")
                    cat = int(input("[Press 1/2]: "))
                    print("\n")

                    if cat < 1 or cat > 2:
                        print("-------------------------------------------------------------")
                        print("Wrong Entry...Please Enter the VALID number and TRY AGAIN..")
                        print("-------------------------------------------------------------")
                        continue
                    else:
                        if op == 1:
                            if name == 1:
                                if cat == 1:
                                    file1 = food[name-1]
                                    append1()
                                elif cat == 2:
                                    file1 = exercise[name-1]
                                    append1()
                                else:
                                    print("Wrong Entry... Please Enter the Correct Number")
                            elif name == 2:
                                if cat == 1:
                                    file1 = food[name-1]
                                    append1()
                                elif cat == 2:
                                    file1 = exercise[name-1]
                                    append1()
                                else:
                                    print("Wrong Entry... Please Enter the Correct Operation Number")
                            elif name == 3:
                                if cat == 1:
                                    file1 = food[name-1]
                                    append1()
                                elif cat == 2:
                                    file1 = exercise[name-1]
                                    append1()
                                else:
                                    print("Wrong Entry... Please Enter the Correct Number")
                            else:
                                print("Wrong Entry... Please Enter the Correct Number")
                        elif op == 2:
                            if name == 1:
                                if cat == 1:
                                    file1 = food[name-1]
                                    ret1()
                                elif cat == 2:
                                    file1 = exercise[name-1]
                                    ret1()
                                else:
                                    print("Wrong Entry... Please Enter the Correct Number")
                            elif name == 2:
                                if cat == 1:
                                    file1 = food[name-1]
                                    ret1()
                                elif cat == 2:
                                    file1 = exercise[name-1]
                                    ret1()
                                else:
                                    print("Wrong Entry... Please Enter the Correct Number")
                            elif name == 3:
                                if cat == 1:
                                    file1 = food[name-1]
                                    ret1()
                                elif cat == 2:
                                    file1 = exercise[name-1]
                                    ret1()
                                else:
                                    print("Wrong Entry... Please Enter the Correct Number")
                            else:
                                print("Wrong Entry... Please Enter the Correct Number")
                        else:
                            print("Wrong Entry... Please Enter the Correct Number")
                    break
                break
            break
        #break
        print("Do you want to perform more operations?")
        k = input("[y/n]:")

        if k == "y":
            continue
        else:
            print("Thank You...Have a Nice Day")
            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