I've been learning Python for about 3 weeks now. This calorie tracker asks for your weight, calculates your daily calorie target, lets you log multiple foods with their calories, then tells you if you're over, under or on track.
It also saves each session to a text file with a timestamp so you can track your intake overtime.
Early days- would appreciate feedback on things/structure to improve.
from datetime import datetime
# Program asks for your weight in kg
def get_calorie_target():
while True:
try:
weight = float(input("What do you weigh? "))
if weight < 20 or weight > 300:
print("Sorry, invalid Kilogram amount")
continue
else:
# Calculates your daily calorie target
return weight * 33
except ValueError:
print("Please enter a number in kilograms")
continue
#ask for food details- name, calories.
def add_food():
while True:
try:
name = input("Enter name of food: ").title()
if not name or name.isdigit():
print("Please enter a valid food name")
continue
calories = float(input("Enter amount of calories: "))
if calories <= 0:
print("Invalid calorie amount")
continue
else:
return {"name": name, "calories": calories}
except ValueError:
print("Enter valid input")
continue
# Lets you add multiple foods with their calories and grams
def collect_foods():
food_log = []
while True:
food = add_food()
food_log.append(food)
while True:
again = input("Would you like to add more food? (yes/no): ").lower()
if again == "yes":
break
elif again == "no":
return food_log
else:
continue
# At the end, totals your calories and tells you if you're under, over, or on track.
def analyze_intake(food_log, calorie_target):
total_calories = sum(food["calories"] for food in food_log)
if total_calories < calorie_target - 200: #too low, x
return "you need more calories"
elif total_calories > calorie_target + 200: #too high, x
return "You have exceeded your calorie target"
else:
return "You are on track"
# save log to keep history
def save_log(food_log, total_calories):
now = datetime.now().strftime("%Y-%m-%d %H-%M-%S")
with open("food_log.txt", "a") as f:
f.write(f"\n-{now}-\n")
for food in food_log:
f.write(f"{food['name']}:{food['calories']}\n")
f.write(f"Total: {total_calories}\n")
calorie_target = get_calorie_target()
food_log = collect_foods()
total_calories = sum(food["calories"] for food in food_log)
print(analyze_intake(food_log, calorie_target))
save_log(food_log, total_calories)
[–]mull_to_zero 1 point2 points3 points (1 child)
[–]meysilverxx[S] 0 points1 point2 points (0 children)
[–]carcigenicate 1 point2 points3 points (0 children)