### 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")
op.write(str([str(gettime())]) + ": " + value + "\n")
print("successfully written")
elif (c == 2):
value = input("type here\n")
op.write(str([str(gettime())]) + ": " + value + "\n")
print("successfully written")
else:
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):
for i in op:
print(i, end="")
elif (c == 2):
for i in op:
print(i, end="")
else:
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)

`````` 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:
print(b)
print("LOGGED SUCESSFULLY! ")
elif name == 2 and routine == 1:
with open("rohandiet.txt", "a") as e:
print(f)
print("LOGGED SUCESSFULLY! ")
elif name == 3 and routine == 1:
print(j)
print("LOGGED SUCESSFULLY! ")

def workout1():
if name == 1 and routine == 2:
with open("marryworkout.txt", "a") as c:
print(d)
print("LOGGED SUCESSFULLY! ")
elif name == 2 and routine == 2:
with open("rohanworkout.txt", "a") as g:
print(h)
print("LOGGED SUCESSFULLY! ")
elif name == 3 and routine == 2:
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:
print(b)
elif name == 2 and routine == 1:
with open("rohandiet.txt") as e:
print(f)
elif name == 3 and routine == 1:
print(j)

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

# Health Management System
"""
Problem Statement:
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")

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

#--------------------------------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?")
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