我正在尝试创建类似于我的日常液体摄入量、食物、运动等的日记。我创建了一些菜单,确实要求用户提供菜单中预定义的特定输入。从那里我需要收集所有输出打印数据并保存到一个文件中,该文件将每天更新(要求用户提供相同的预定义输入。
代码很长,但看起来像这样:
health = open('healthy.txt','a+')
date = input('Date: ')
#Loop Menu for Exercise info for that day.
loop=True
while (loop):
print ()
print ("----------------------------------")
print ("1 = Yoga")
print ("2 = Cardio")
print ("3 = Walk")
print ("4 = Dance")
print ("5 = Rest/None")
print ("D = Done")
print ("----------------------------------")
print ()
exer = input('Exercise: ')
if (exer =='1'):
print("Yoga")
elif (exer =='2'):
print("Cardio")
elif (exer =='3'):
print ("Walk")
elif (exer =='4'):
print ("Dance")
elif (exer == '5'):
print ("Rest/None")
elif (exer =='d' or exer =='D'):
loop=False
else:
print ("Invalid Option")
#End Exercise Menu
dur = input('Duration(in minutes): ')
#Loop menu for the Food Intake for particular date.
loop=True
while (loop):
print ()
print ("----------------------------------")
print ("1 = Protein")
print ("2 = Starch")
print ("3 = Fruit")
print ("4 = Vegetable")
print ("D = Done")
print ("----------------------------------")
print ()
food = input('Food: ')
if (food =='1'):
print("Protein")
elif (food =='2'):
print("Starch")
elif (food =='3'):
print ("Fruit")
elif (food =='4'):
print ("Vegetable")
elif (food =='d' or food =='D'):
loop=False
else:
print ("Invalid Option")
#End Food Menu
fluid = input('Fluid Intake Total: ')
#for line in health:
#Final print of info to be written to file
#not sure if I need the next four lines
food_true = False
food_total = 0
exer_true = False
exer_total = 0
for char in exer:
if (exer_true == "Yoga"):
print ("Yoga", end=" ")
if (exer_true == "Cardio"):
print ("Cardio", end=" ")
if (exer_true == "Walk"):
print ("Walk", end= " ")
if (exer_true == "Dance"):
print ("Dance",end=" ")
if (exer_true == "Rest/None"):
print ("Rest/None",end=" ")
else:
loop=False
while(loop):
if (food_true == "Protein"):
print ("Protein", end=" ")
if (food_true == "Starch"):
print ("Starch", end=" ")
if (food_true == "Fruit"):
print ("Fruit", end= " ")
if (food_true == "Vegetable"):
print ("Vegetable",end=" ")
else:
loop=False
print ("{:>10}".format("Date: "),date)
print ("{:>10}".format("Exercises: "),exer_total)
print ("{:>10}".format("Duration: "),dur)
print ("{:>10}".format("Foods: "),food_total)
print ("{:>10}".format("Fluids: "),fluid)
print("-"*60)
#Save/Write to file function will be here
#Save/Write to file function will be here
health.write(info)#word "info" is a place holder only for final code info
health.close()
我希望能够仅打印出信息(这不包括将信息保存到文件中),以在一个整洁的文件中(格式并不重要,只要它是整洁的):日期:输出;练习:输出(所有如果适用);持续时间:输出;食物:输出(所有四个如果适用);流体:输出
每天我都会运行程序并更新上述所有内容的每日信息。然后文件需要每天将信息写入并保存到同一个文件中(不能每天都是新文件,因此在代码开头使用以下内容,这是必须的): health = open('健康.txt','a+')