-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
203 lines (162 loc) · 5.13 KB
/
project.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import re
import csv
import random
class User:
def __init__(self, age=0, gender=0, weight=0, height=0, favorite=0, activity=0):
self.age = age
self.gender = gender
self.weight = weight
self.height = height
self.favorite = favorite
self.activity = activity
food_list = []
user = User()
def main():
breakfast = []
lunch = []
dinner = []
global food_list
check_gender()
age = input("Please enter your age in numbers (eg: 18): ")
check_age(age)
height = check_height()
weight = check_weight()
check_active()
bmr = BMR()
bmi = Bmi(height, weight)
value = Bmi_range(bmi)
calories = calorie_calc(bmr)
calories = int(calories)
print()
print(f"Basal Metabolic rate: {bmr}")
print(f"Needed calories: {calories}")
print(f"Body Mass Index: {bmi}")
print(f"According to your BMI, you are {value}")
# Calories for meals
bf_cal = calories * 2 / 3
ln_cal = calories * 1 / 3
# Send file name
check_food("nutrients_csvfile.csv")
# Input 1/3 of calories into breakfast list
while calories > bf_cal:
num = random.randint(1, 300)
breakfast.append(
{"food": food_list[num]["Food"], "grams": food_list[num]["Grams"]}
)
calories = calories - int(food_list[num]["Calories"].replace(",", ""))
# Input 1/3 of calories into lucnh list
while calories > ln_cal:
num = random.randint(1, 300)
lunch.append({"food": food_list[num]["Food"], "grams": food_list[num]["Grams"]})
calories = calories - int(food_list[num]["Calories"].replace(",", ""))
# Input 1/3 of calories into dinner list
while calories > 0:
num = random.randint(1, 300)
dinner.append(
{"food": food_list[num]["Food"], "grams": food_list[num]["Grams"]}
)
calories = calories - int(food_list[num]["Calories"].replace(",", ""))
# Print breakfast meals
print("Breakfast: ")
for item in breakfast:
print(f"- {item['grams']} gm {item['food']} ")
# Print Lunch meals
print("Lunch: ")
for item in lunch:
print(f"- {item['grams']} gm {item['food']} ")
# Print Dinner meals
print("Dinner: ")
for item in dinner:
print(f"- {item['grams']} gm {item['food']} ")
def check_age(age):
if Age := re.search(r"^([0-9][0-9]?)$", age):
global user
user.age = Age.group(1)
return True
else:
return check_age(input("Enter a valid age: "))
def check_gender():
global user
gender = input("Enter your Gender: ")
if Gender := re.search(r"^(M|F|MALE|FEMALE)$", gender.upper()):
user.gender = Gender.group().lower()
return Gender.group()
else:
print("Enter a valid gender")
return check_gender()
def check_height():
global user
height = input("Enter your height in Cms: ")
if height := re.search(r"^([1-9][0-9][0-9]?)$", height):
user.height = height.group()
return height.group()
else:
print("Enter a valid height")
return check_height()
def check_weight():
global user
weight = input("Enter your weight in kilograms: ")
if weight := re.search(r"^([0-9][0-9]?[0-9]?)$", weight):
user.weight = weight.group()
return weight.group()
else:
print("Enter a valid weight")
return check_weight()
def check_active():
global user
activity = input(
"From a scale 1-5, How active are you?, 1 means sedentary life and 5 means very active. : "
)
if activity := re.search(r"^([12345])$", activity):
user.activity = activity.group()
return activity.group()
else:
print("Enter a valid activity rate")
return check_active()
possible = check_food(ls)
return possible
def check_food(file):
global food_list
global user
ls = []
with open(file) as f:
read = csv.DictReader(f)
for line in read:
food_list.append(line)
return True
def BMR():
# If user is a male
if user.gender in ["male", "m"]:
bmr = (
66.5
+ (13.75 * int(user.weight))
+ (5.003 * int(user.height))
- (6.75 * int(user.age))
)
# If user is a female
else:
bmr = (
655.1
+ (9.563 * int(user.weight))
+ (1.850 * int(user.height))
- (4.676 * int(user.age))
)
return bmr
def calorie_calc(bmr):
calories = bmr * (1.2 + ((int(user.activity) - 1) * 0.175))
return calories
def Bmi(height, weight):
return int(weight) / (int(height) / 100) ** 2
def Bmi_range(bmi):
bmi = float(bmi)
if bmi < 18.5:
value = "Underweight"
elif bmi >= 18.5 and bmi < 24.9:
value = "Healthy"
elif bmi >= 25.0 and bmi < 29.9:
value = "Overweight"
if bmi >= 30.0:
value = "Obese"
return value
if __name__ == "__main__":
main()