python - 每个日历日期执行功能

标签 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

最佳答案

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

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

您对这个项目的功能抱有很大的野心,但如果您使用日期时间库并通读文档,您也将能够弄清楚调度位。

关于python - 每个日历日期执行功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52543722/

相关文章:

python - NetworkX - 生成随机连接的二分图

python - 如何在pygame中获取图片大小(python)

python - 利克瑞尔数算法

python - Python 如何查看另一个客户端对 MariaDB 所做的更改?

python - NLP:为什么在 sklearn Pipeline 中使用两个向量化器(Bag of Words/TFIDF)?

python - 类型错误 : list indices must be integers, 不是 numpy.float64

python - 解释 GridSearchCV 中的岭回归

python - Paraview:我可以通过 python 脚本注释源吗?

python - 是否可以在 Google App Engine 中使用 textile 2.2.0?

python - 使用Python通过 turtle 引用列表或字典绘制正弦波