使用Python的健康管理系统
有时我们忙得连自己的身体都照顾不上,所谓的照顾就是健身、食物、运动等等,然后我们认为我们需要制定我们的饮食计划或锻炼计划来保持我们的身体。所以让我们制作一个Python脚本来为我们维护这个记录。在这个程序中,我们添加了我们需要做的饮食和运动,或者我们可以保存我们每天的饮食或运动,并带有日期和时间来记录。它还可以帮助我们制定另一个健身计划。
在这个程序中,用户输入他们的饮食和运动,然后我们将输入的日期和时间保存起来,这样用户无论何时都可以看到他们的日常活动。在此情况下将执行以下操作:
- 获取日期()
- 选择名称()
- select_file_action()
- 选择任务()
- 行动()
方法:
- getdate():跟踪数据的日期,以便日志检索有意义
Python3
def getdate():
# to get date and time
return datetime.datetime.now()
Python3
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
Python3
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
Python3
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
Python3
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)
Python
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)
- selectname():这个函数让用户选择要执行所需任务的人。该函数首先提供在两个人之间进行选择的选项,并将输入作为整数。然后将其返回以进行进一步处理。
蟒蛇3
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():这个函数接受应该对文件做的事情,即文件应该用于写入数据(日志)或检索现有数据。这再次接受整数参数并返回它们。
蟒蛇3
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():此函数有助于指定与必须输入的任务相关的数据。这里给出的选择是在食物和运动之间。这再次接受整数输入并将其返回以进行进一步处理。
蟒蛇3
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():通过条件检查根据上述函数返回的输入执行适当的任务。
蟒蛇3
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)
下面是一个完整的实现。
Python
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)
输出: