I'm new to coding, and this is the first useful code ive made for myself
hi, ive recently started to learn Python as my first coding language. And to be honest, i wasn't really serious about it. Because i used to do simple stuff that i enjoyed such as a Cubic Equation Solver. It may not be as efficient (not including imports which directly solve it), but it was fun.
but finally, i tried to do something which is useful for me. That is, a calorie counting program.
i believe i spent like 5 hours continuosly, really invested, and learning new terms.
I still plan on adding more features such as:
Including Fats
Increase food dictionary
allow plural forms of food.
Adding more words for "yes"
Make sub-dictionaries for different types of foods, with sub-dictionaries within it for servings (might get too complicated)
Feel free to test it out!
def float_checker(val):
while True:
try:
return float(input(val))
except:
print("Please enter a number!")
maintenance_cal = round(float_checker("Enter your Daily Maintenance Calories: "))
deficit_cal = round(float_checker("What is your Daily Target Deficit? "))
body_weight = float_checker("Enter you Body Weight (Kg): ")
target_calories = maintenance_cal - deficit_cal
req_pro = round(2 * body_weight)
fat_loss_calc_weekly = round(deficit_cal / 1.1)
deficit_percent = round((100 * deficit_cal / maintenance_cal))
repeat_text = f'Your Deficit Percentage: {deficit_percent}% is '
if deficit_percent < 0:
print(f"{repeat_text}in the negatives! You are GAINING calories.")
elif 0 <= deficit_percent <10:
print(f"{repeat_text}Mild for fat loss.")
elif 10 <= deficit_percent < 20:
print(f"{repeat_text}Optimal for fat loss.")
elif 20 <= deficit_percent < 30:
print(f"{repeat_text}Aggressive for short-term fat loss.")
elif 30 <= deficit_percent:
print(f"{repeat_text}DANGEROUS! Please increase your intake!")
print(f"Approximate Weekly Fat Loss = {fat_loss_calc_weekly}g")
foods = {
#SERVING SIZE: 1 UNIT
"egg": {"calories": 80, "protein": 6}, "chicken breast": {"calories": 145, "protein": 36},
#SERVING SIZE: 1 SCOOP : 35g
"whey": {"calories": 125, "protein": 25},
#SERVING SIZE: 1mL/1g
"curds": {"calories": 0.65, "protein": 0.04}, "milk": {"calories": 0.5, "protein": 0.04},
#SERVING SIZE: 1 Heap tbsp
"peanut butter": {"calories": 165, "protein": 7}, "hummus": {"calories": 35, "protein": 1.5},
}
print("NEW DAY: ")
print(f"TODAY PROTEIN REQUIREMENT: {req_pro}g")
print(f"TODAY CALORIE REQUIREMENT: {target_calories}kcal")
sum_cal = 0
sum_pro = 0
yes_response = ["yes", "yep", "y" , "ok" , "okay", "ye"]
confirmation = input("Have you had anything? ").lower()
while confirmation in yes_response:
food = input("What have you had? ").lower()
if food not in foods:
print("Enter a valid food!")
continue
else:
servings = float_checker("How many servings? [1 UNIT / 1 SCOOP / 1 mL / 1 HEAPED TBSP] ")
f_cal = foods[food]["calories"]
f_pro = foods[food]["protein"]
sum_cal = round(f_cal * servings + sum_cal)
sum_pro = round(f_pro * servings + sum_pro)
print(f"{sum_cal} kcal consumed today")
if sum_cal >= target_calories:
print(f"You've exceeded your caloric intake by {sum_cal - target_calories} kcal!")
else:
print(f"{target_calories - sum_cal} kcal remaining today")
print(f"{sum_pro} g Protein consumed today")
if sum_pro >= req_pro:
print(f"You've crossed your Protein Requirement by {sum_pro - req_pro}g!")
else:
print(f"{req_pro - sum_pro} g Protein remaining")
confirmation = input("Have you had anything else? ").lower()
if confirmation not in yes_response:
print("Good Luck!")