Sometimes we are so busy that we are not even able to care for our body and by caring we mean fitness, food, exercises and much more, and then we think that we need to make our diet plan or exercise plan to maintain our body. So let’s make a Python script to maintain this record for us. In this program, we add our diet and exercises that we need to do, or we can save our daily diet or exercise with date and time to keep records. It also helps us to make another fitness plan.
In this program, user input their diet and exercise, and then we save the input with the date and time, so whenever the user wants they can see their routine. Following operations will be performed under this:
- getdate()
- selectname()
- select_file_action()
- select_task()
- action()
Approach:
- getdate(): Tracks date for data so that log retrieval can make sense
def getdate():
# to get date and time
return datetime.datetime.now()
|
- selectname(): This function lets the user select the person on which the required tasks are to be performed. This function first gives options to choose between two people and takes the input as an integer. This is then returned for further processing.
def selectname():
name = { 1 : "Nilesh" , 2 : "Shanu" }
b = { 1 : "Food" , 2 : "Exercise" }
for key, value in name.items():
# taking input of name
print ( "Press" , key, "for" , value, "\n" , end = "")
n = int ( input ( "type here.." ))
if n > 2 :
print ( "error select 1 or 2" )
exit()
else :
return n
|
- select_file_action(): This function accepts what should be done to the file i.e. either the file should be used to write the data (log) or retrieve the existing data. This again takes integer arguments and returns them.
def select_file_action():
a = { 1 : "Log" , 2 : "Retrieve" }
for key, value in a.items():
# taking input of function that user wants to
# do (either log or retrieve)
print ( "Press" , key, "for" , value, "\n" , end = "")
x = int ( input ( "type here.." ))
if x > 2 :
print ( "error select 1 or 2" )
exit()
else :
return x
|
- select_task(): This function helps specify data related to what task has to be entered. The choice here given is between food and exercise. This again accepts integer input and returns it for further processing.
def select_task():
b = { 1 : "Food" , 2 : "Exercise" }
for key, value in b.items():
# ask user to choose between food and exercise
print ( "Press" , key, "for" , value, "\n" , end = "")
y = int ( input ( "type here.." ))
if y > 2 :
print ( "error select 1 or 2" )
exit()
else :
return y
|
- action(): Performs appropriate task according to the inputs returned by above functions by condition checking.
def action(n, x, y):
# condition no 1
if n = = 1 and x = = 1 and y = = 1 :
value = input ( "type here\n" )
with open ( "nilesh food.txt" , "a" ) as nileshfood:
# printing date and time
nileshfood.write( str ([ str (getdate())]) + ": " + value + "\n" )
print ( "successfully written" )
# condition no 2
elif n = = 1 and x = = 1 and y = = 2 :
value = input ( "type here\n" )
# printing date and time
with open ( "nilesh exercise.txt" , "a" ) as nileshexercise:
# printing date and time
nileshexercise.write( str ([ str (getdate())]) + ": " + value + "\n" )
print ( "successfully written" )
# condition 3
elif n = = 2 and x = = 1 and y = = 1 :
value = input ( "type here\n" )
# printing date and time
with open ( "shanu food.txt" , "a" ) as shanufood:
# printing date and time
shanufood.write( str ([ str (getdate())]) + ": " + value + "\n" )
print ( "successfully written" )
# condition 4
elif n = = 2 and x = = 1 and y = = 2 :
value = input ( "type here\n" )
# printing date and time
with open ( "shanu exercise.txt" , "a" ) as shanuexercise:
# printing date and time
shanuexercise.write( str ([ str (getdate())]) + ": " + value + "\n" )
print ( "successfully written" )
# condition 5
elif n = = 1 and x = = 2 and y = = 1 :
# printing date and time
with open ( "nilesh food.txt" , "r" ) as nileshfood:
a = nileshfood.read()
print (a)
# condition no 6
elif n = = 1 and x = = 2 and y = = 2 :
# printing date and time
with open ( "nilesh exercise.txt" , "r" ) as nileshexercise:
a = nileshexercise.read()
print (a)
# condition no 7
elif n = = 2 and x = = 2 and y = = 1 :
# printing date and time
with open ( "shanu food.txt" , "r" ) as shanufood:
a = shanufood.read()
print (a)
# condition no 8
elif n = = 2 and x = = 2 and y = = 2 :
# printing date and time
with open ( "shanu exercise.txt" , "r" ) as shanuexercise:
a = shanuexercise.read()
print (a)
|
Below is a complete implementation.
import datetime
def getdate():
# to get date and time
return datetime.datetime.now()
def selectname():
name = { 1 : "Nilesh" , 2 : "Shanu" }
b = { 1 : "Food" , 2 : "Exercise" }
for key, value in name.items():
# taking input of name
print ( "Press" , key, "for" , value, "\n" , end = "")
n = int ( input ( "type here.." ))
if n > 2 :
print ( "error select 1 or 2" )
exit()
else :
return n
def select_file_action():
a = { 1 : "Log" , 2 : "Retrieve" }
for key, value in a.items():
# taking input of function that user wants to
# do (either log or retrieve)
print ( "Press" , key, "for" , value, "\n" , end = "")
x = int ( input ( "type here.." ))
if x > 2 :
print ( "error select 1 or 2" )
exit()
else :
return x
def select_task():
b = { 1 : "Food" , 2 : "Exercise" }
for key, value in b.items():
# ask user to choose between food
# and exercise
print ( "Press" , key, "for" , value, "\n" , end = "")
y = int ( input ( "type here.." ))
if y > 2 :
print ( "error select 1 or 2" )
exit()
else :
return y
def action(n, x, y):
# condition no 1
if n = = 1 and x = = 1 and y = = 1 :
value = input ( "type here\n" )
with open ( "nilesh food.txt" , "a" ) as nileshfood:
# printing date and time
nileshfood.write( str ([ str (getdate())]) + ": " + value + "\n" )
print ( "successfully written" )
# condition no 2
elif n = = 1 and x = = 1 and y = = 2 :
value = input ( "type here\n" )
# printing date and time
with open ( "nilesh exercise.txt" , "a" ) as nileshexercise:
# printing date and time
nileshexercise.write( str ([ str (getdate())]) + ": " + value + "\n" )
print ( "successfully written" )
# condition 3
elif n = = 2 and x = = 1 and y = = 1 :
value = input ( "type here\n" )
# printing date and time
with open ( "shanu food.txt" , "a" ) as shanufood:
# printing date and time
shanufood.write( str ([ str (getdate())]) + ": " + value + "\n" )
print ( "successfully written" )
# condition 4
elif n = = 2 and x = = 1 and y = = 2 :
value = input ( "type here\n" )
# printing date and time
with open ( "shanu exercise.txt" , "a" ) as shanuexercise:
# printing date and time
shanuexercise.write( str ([ str (getdate())]) + ": " + value + "\n" )
print ( "successfully written" )
# condition 5
elif n = = 1 and x = = 2 and y = = 1 :
# printing date and time
with open ( "nilesh food.txt" , "r" ) as nileshfood:
a = nileshfood.read()
print (a)
# condition no 6
elif n = = 1 and x = = 2 and y = = 2 :
# printing date and time
with open ( "nilesh exercise.txt" , "r" ) as nileshexercise:
a = nileshexercise.read()
print (a)
# condition no 7
elif n = = 2 and x = = 2 and y = = 1 :
# printing date and time
with open ( "shanu food.txt" , "r" ) as shanufood:
a = shanufood.read()
print (a)
# condition no 8
elif n = = 2 and x = = 2 and y = = 2 :
# printing date and time
with open ( "shanu exercise.txt" , "r" ) as shanuexercise:
a = shanuexercise.read()
print (a)
n = selectname()
x = select_file_action()
y = select_task()
action(n, x, y) |
Output: