-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacrotracker.py
More file actions
91 lines (73 loc) · 2.44 KB
/
macrotracker.py
File metadata and controls
91 lines (73 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def get_numeric_value(prompt):
while True:
raw_value = input(prompt).strip()
try:
return float(raw_value)
except ValueError:
print("Invalid input. Please enter a numerical value.")
def macro_entry():
entries = []
while True:
food_name = input("Please enter name of food (or type 'done' to finish): ").strip()
if not food_name:
print("Food name cannot be empty.")
continue
if food_name.lower() == "done":
break
print(f"You have entered: {food_name}")
calories = get_numeric_value("How many calories: ")
protein = get_numeric_value("How much protein: ")
fat = get_numeric_value("How much fat: ")
carbs = get_numeric_value("How much carbs: ")
entries.append(
{
"food": food_name,
"calories": calories,
"protein": protein,
"fat": fat,
"carbs": carbs,
}
)
return entries
def print_table(entries):
if not entries:
print("\nNo food entries were added.")
return
headers = ["Food", "Calories", "Protein", "Fat", "Carbs"]
rows = [
[
item["food"],
f"{item['calories']:.2f}",
f"{item['protein']:.2f}",
f"{item['fat']:.2f}",
f"{item['carbs']:.2f}",
]
for item in entries
]
widths = [len(header) for header in headers]
for row in rows:
for i, cell in enumerate(row):
widths[i] = max(widths[i], len(cell))
def format_row(row_values):
return " | ".join(value.ljust(widths[i]) for i, value in enumerate(row_values))
print("\nMacro Tracker Table")
print(format_row(headers))
print("-+-".join("-" * width for width in widths))
for row in rows:
print(format_row(row))
total_calories = sum(item["calories"] for item in entries)
total_protein = sum(item["protein"] for item in entries)
total_fat = sum(item["fat"] for item in entries)
total_carbs = sum(item["carbs"] for item in entries)
total_row = [
"TOTAL",
f"{total_calories:.2f}",
f"{total_protein:.2f}",
f"{total_fat:.2f}",
f"{total_carbs:.2f}",
]
print("-+-".join("-" * width for width in widths))
print(format_row(total_row))
if __name__ == "__main__":
all_entries = macro_entry()
print_table(all_entries)