我目前正在开发一个营养计划器,它允许用户按日历日计划膳食。作为演示,我从一个 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))
我如何让用户选择一个日期然后执行此功能?
这样,例如,可以为每一天分配一个 lunchName
和 dinnerName
。
最佳答案
您可以使用 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/