每个日历日期执行功能

时间:2018-09-27 19:15:32

标签: python

我目前正在研究营养计划器,该程序将允许用户每个日历日计划膳食。作为一个演示,我从JSON文件中加载了数据,包括餐食名称和卡路里,允许用户选择他们想要的餐食,等等。我希望他们每天都能做到并组织其中日历。

这是我为膳食计划者编写的代码,该代码可以按我希望的方式工作:

breakfastInput = input("What would you like for breakfast? ")
breakfastChoice = [breakfastChoice for breakfastChoice in breakfast if breakfastInput in breakfastChoice][0]
breakfastName = breakfastChoice[0]
breakfastCalories = breakfastChoice[1]
calories = calories - breakfastCalories
print("You have {} calories remaining.".format(calories))

lunchInput = input("What would you like for lunch? ")
lunchChoice = [lunchChoice for lunchChoice in lunch if lunchInput in lunchChoice][0]
lunchName = lunchChoice[0]
lunchCalories = lunchChoice[1]
calories = calories - lunchCalories
print("You have {} calories remaining.".format(calories))

dinnerInput = input("What would you like for dinner? ")
dinnerChoice = [dinnerChoice for dinnerChoice in dinner if dinnerInput in dinnerChoice][0]
dinnerName = dinnerChoice[0]
dinnerCalories = dinnerChoice[1]
calories = calories - dinnerCalories
print("You have {} calories remaining.".format(calories))

if input("Would you like any snacks? ") == "yes":
    print(snacks)
    snacksInput = input("What snack would you like? ")
    snacksChoice = [snacksChoice for snacksChoice in snacks if snacksInput in snacksChoice][0]
    snacksName = snacksChoice[0]
    snacksCalories = snacksChoice[1]
    calories = calories - snacksCalories
    print("You have {} calories remaining.".format(calories))
else:
    print("Your day has been planned with a remaining {} calories.".format(calories))

如何允许用户选择日期,然后执行此功能?

例如,可以将lunchNamedinnerName分配给每一天。

1 个答案:

答案 0 :(得分:2)

您可以使用python's datetime library。举个简单的例子:

from datetime.datetime import strptime
lunchDateInput = input("What date are you having lunch on?")
lunchDate = strptime(lunchDateInput, "MM/DD/YYYY")

您对该项目的功能抱有很大的野心,但是如果您使用datetime库并阅读文档,那么您也可以弄清楚调度位。