-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmi_calculator_github
426 lines (426 loc) · 23.4 KB
/
bmi_calculator_github
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"id": "17d41787",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to Body Mass Index Caluculation program. \n",
"This program will tell you if you have a healthy weight or not. \n",
"For more information on Body mass index visit: \n",
"https://www.euro.who.int/en/health-topics/disease-prevention/nutrition/a-healthy-lifestyle/body-mass-index-bmi\n",
"\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-12-132d8870ef9c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 299\u001b[0m \u001b[0;31m# A delay is used to prevent the input from being printed before the\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 300\u001b[0m \u001b[0;31m# welcoming message.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 301\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 302\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 303\u001b[0m \u001b[0;31m# The main function of the program.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"# BMI caclulator \n",
"# Stany Vanhemelrijck\n",
"# VAN20500599\n",
"# Last updated on 26 okt 2021\n",
"\n",
"# This line of numbers is to count up to 79 characters.\n",
"# Following the guidelines of pep 8, the maximum number of characters on \n",
"# one line increases readability on smaller screens or when screens are \n",
"# reduced in size. \n",
"# The only place where this was not followed was when a long string was \n",
"# printed out. In that case, clear formatting of what the user sees was chosen\n",
"# above the readability of the code.\n",
"# I tried to make it more readable by creating a multiline string,\n",
"# but for some reason, the indentation on the second and following lines had\n",
"# to be deleted, or the format of the output was not correct. \n",
"#234567891123456789212345678931234567894123456789512345678961234567897123456789\n",
"\n",
"# The time module is necessary for this program, used on line 236.\n",
"import time\n",
"\n",
"\n",
"# This BMI calculator program consists of 2 functions. \n",
"# One for the imperial system and one for the metric system, which includes: \n",
"# Asking for the user's input, calculating their BMI and printing out the \n",
"# result and BMI category defined by the World Health Organisation. \n",
"# In the 'main program' loop, we ask the user which measurement system they \n",
"# want to use. Depending on their choice, the program runs the \n",
"# corresponding function. \n",
"# I choose to write two different functions because of the different \n",
"# calculations of the BMI depending on the measurement system. \n",
"# One function or one loop that would choose the corresponding sentence or \n",
"# calculation might have worked. However, this seemed too complicated for \n",
"# writing the code and altering the code in the future. \n",
"# One of the cons to my approach was when I changed something in one \n",
"# function, I had to make the same change in the other function, \n",
"# or risk inconsistency. \n",
"\n",
"\n",
"# Defining the function for the imperial measurement system. \n",
"def imperial():\n",
" # The while loop makes sure that when wrong input is given, the program \n",
" # asks again.\n",
" condition = True\n",
" while condition == True:\n",
" # The try statement is used in combination with the except statement\n",
" # When something else than a number is given, the program show a \n",
" # error message saying 'Please enter numbers.'\n",
" try: \n",
" # Asking for the weight from the user and convert it to a float,\n",
" # so this can be used for numeric calculations,\n",
" # and asign the input to the imperial_weight variable. \n",
" # Working with asking for input, followed by the IF statement, \n",
" # makes sure that as soon as the user inputs 0, \n",
" # a message is printed. Not after both inputs are over.\n",
" imperial_weight = float(\n",
" input('Insert your weight in pounds and press enter: '))\n",
"\n",
" # if the users enters 0 in weight an error message is printed\n",
" # and the function runs again. \n",
" if imperial_weight == 0: \n",
" print('\\nYour weight can not be 0.')\n",
" imperial()\n",
" # This break statement is explained on line 75.\n",
" break\n",
"\n",
" # Asking for the height from the user and convert it to a float. \n",
" imperial_height = float(\n",
" input('Insert your height in inches and press enter: '))\n",
" \n",
" # if the users enters 0 in height an error message is printed\n",
" # and the function runs again. \n",
" if imperial_height == 0: \n",
" print('\\nYour height can not be 0.')\n",
" imperial()\n",
" # In the situation that a correct input was given for weigth\n",
" # but a zero was given for height, the program would ask for\n",
" # input ones more. When correct input was given for both \n",
" # inputs the calculation would print, but the loop to ask \n",
" # the user if their input is correct would keep running for\n",
" # for the previous loop where a zero was entered. \n",
" # This break statement prevents that error. \n",
" break\n",
"\n",
" # When a ValueError is raised. I.e. when something else than a number \n",
" # is given as an input the program shows an error message. \n",
" except ValueError:\n",
" print('\\nPlease enter numbers.')\n",
" imperial()\n",
"\n",
" \n",
" # Asking the user to check the input and to confirm.\n",
" # the \\n in the first print statement, makes sure there is an empty \n",
" # line between the previous print statement and this one. \n",
" # For formatting reasons. \n",
" print('\\nYour weight is: ', imperial_weight, ' pounds.')\n",
" print('Your height is: ', imperial_height, ' inches.')\n",
"\n",
" # while loop until the user gives confirmation that the input \n",
" # is correct.\n",
" # The condition of the while loop is set to False when the \n",
" # answer is yes, to exit the while loop and continue the rest of \n",
" # the code block. \n",
" condition = True\n",
" while condition == True:\n",
" # Asking the user to enter YES if the input is correct or \n",
" # NO if they want to alter their input.\n",
" correct = input('Is this correct? \\nType YES if you want to continue or type NO if you would like to alter your answers. And press ENTER.\\n')\n",
" # if the user enters yes, the condition is set to False.\n",
" # as to exit the loop and execute the rest of the code. \n",
" # .lower() to make sure when the user enters Yes or YES it converts \n",
" # to yes.\n",
" # .strip() is used to remove leading and trailing spaces. \n",
" # So that ' yes' or 'yes ' is also accepted.\n",
" if correct.lower().strip() == 'yes':\n",
" condition = False\n",
" # if the user enters no, run the function again = ask for new input.\n",
" elif correct.lower().strip() == 'no':\n",
" imperial()\n",
" else:\n",
" # if anything else is inputted, ask for yes or no only.\n",
" print('\\nEnter either YES or NO')\n",
"\n",
" \n",
" # BMI calculation.\n",
" # The calculation is executed unless there is an error.\n",
" try: \n",
" # The bmi calculation for the imperial system is: \n",
" # (weight / by height^2) * 703.\n",
" # Using round(..., 2) to round up the result to 2 decimal points.\n",
" imperial_bmi = round(\n",
" (imperial_weight / (imperial_height**2))*703, 2)\n",
" # The result is printed to the user. \n",
" print('\\nYour Body mass index = ',imperial_bmi)\n",
" \n",
" # if statement that decides the category of the bmi.\n",
" # if the bmi is smaller than 18.5, the category is underweight.\n",
" if imperial_bmi < 18.5:\n",
" print('Your category according to the WHO (World Health Organization) is: Underweight')\n",
" # if the bmi is bigger or equal to 18.5 and smaller than or \n",
" # equal to 29.9, the category is pre-obesity.\n",
" elif imperial_bmi >= 25 and imperial_bmi <= 29.9:\n",
" print('Your category according to the WHO (World Health Organization) is: Pre-obesity')\n",
" # if the bmi is bigger or equal to 30 and smaller than or equal to \n",
" # 34.9, the category is obesity class 1. \n",
" elif imperial_bmi >= 30 and imperial_bmi <= 34.9:\n",
" print('Your category according to the WHO (World Health Organization) is: Obesity class 1')\n",
" # if the bmi is bigger than or equal to 30 and smaller than \n",
" # or equal to 39.9, the category is obesity class 2.\n",
" elif imperial_bmi >= 35 and imperial_bmi <= 39.9:\n",
" print('Your category according to the WHO (World Health Organization) is: Obesity class 2')\n",
" # if the bmi is bigger or equal to 40, the category is \n",
" # obesity class 3.\n",
" elif imperial_bmi >= 40:\n",
" print('Your category according to the WHO (World Health Organization) is: Obesity class 3')\n",
" # In any other case the category is normal\n",
" else:\n",
" print('Your category according to the WHO (World Health Organization) is: Normal')\n",
"\n",
" # if the user enters 0 in height the program breaks. \n",
" except ZeroDivisionError:\n",
" print('Your height can not be 0. Re-run the program.')\n",
" break\n",
"\n",
" \n",
"# Defining the function for the imperial measurement system. \n",
"# Every comment in the metric function is the same as in the imperial \n",
"# function. \n",
"def metric():\n",
" # The while loop makes sure that when wrong input is given, the program \n",
" # asks again.\n",
" condition = True\n",
" while condition == True:\n",
" # The try statement is used in combination with the except statement\n",
" # When something else than a number is given, the program show a \n",
" # error message saying 'Please enter numbers.'\n",
" try: \n",
" # Asking for the weight from the user and convert it to a float,\n",
" # so this can be used for numeric calculations,\n",
" # and asign the input to the imperial_weight variable. \n",
" # Working with asking for input, followed by the IF statement, \n",
" # makes sure that as soon as the user inputs 0, \n",
" # a message is printed. Not after both inputs are over.\n",
" metric_weight = float(input('Insert your weight in kilograms and press enter: '))\n",
" \n",
" # if the users enters 0 in weight an error message is printed\n",
" # and the function runs again. \n",
" if metric_weight == 0: \n",
" print('\\nYour weight can not be 0.')\n",
" metric()\n",
" # This break statement is explained on line 75.\n",
" break\n",
"\n",
" # Asking for the height from the user and convert it to a float. \n",
" metric_height = float(input('Insert your height in meters (use . instead of ,) and press enter: '))\n",
" \n",
" # if the users enters 0 in height an error message is printed\n",
" # and the function runs again. \n",
" if metric_height == 0: \n",
" print('\\nYour height can not be 0.')\n",
" metric()\n",
" # In the situation that a correct input was given for weigth\n",
" # but a zero was given for height, the program would ask for\n",
" # input ones more. When correct input was given for both \n",
" # inputs the calculation would print, but the loop to ask \n",
" # the user if their input is correct would keep running for\n",
" # for the previous loop where a zero was entered. \n",
" # This break statement prevents that error. \n",
" break\n",
"\n",
" # When a ValueError is raised. I.e. when something else than a number \n",
" # is given as an input the program shows an error message. \n",
" except ValueError:\n",
" print('\\nPlease enter numbers. If you enter decimals, make sure to use a point. E.g: 1.72')\n",
" metric()\n",
" \n",
" # Asking the user to check the input and asking for confirmation. \n",
" # the \\n in the first print statement, makes sure there is an empty \n",
" # line between the previous print statement and this one. \n",
" # For nice formatting reasons. \n",
" print('\\nYour weight is: ', metric_weight, ' kilograms.')\n",
" print('Your height is: ', metric_height, ' meters.')\n",
" \n",
" \n",
" # while loop until the user gives confirmation that the input is \n",
" # correct. \n",
" # The condition of the while loop is set to False when the \n",
" # answer is yes, to exit the while loop and continue the rest of \n",
" # the code block. \n",
" condition = True\n",
" while condition == True:\n",
" # Asking the user to enter YES if the input is correct or \n",
" # NO if they want to alter their input.\n",
" correct = input(\"\"\"Is this correct? \\nType YES if you want to continue or type NO if you would like to alter your answers. And press ENTER.\\n\"\"\")\n",
" # if the user enters yes, the condition is set to False.\n",
" # as to exit the loop and execute the rest of the code. \n",
" # .lower() to make sure when the user enters Yes or YES it converts \n",
" # to yes.\n",
" # .strip() is used to remove leading and trailing spaces. \n",
" # So that ' yes' or 'yes ' is also accepted.\n",
" if correct.lower().strip() == 'yes':\n",
" condition = False\n",
" # if the user enters no, run the function again \n",
" # = ask for new input.\n",
" elif correct.lower().strip() == 'no':\n",
" metric()\n",
" else:\n",
" # if anything else is inputted, ask for yes or no only.\n",
" print('\\nEnter either YES or NO')\n",
" \n",
" \n",
" # BMI calculation.\n",
" # The calculation is executed unless there is an error.\n",
" try: \n",
" # The bmi calculation for the metric system is: \n",
" # (weight / by height^2).\n",
" # Using round(..., 2) to round up the result to 2 decimal points.\n",
" metric_bmi = round(metric_weight / (metric_height**2), 2)\n",
" # The result is printed to the user. \n",
" print('\\nYour Body mass index = ',metric_bmi)\n",
" \n",
" # if statement that decides the category of the bmi.\n",
" # if the bmi is smaller than 18.5, the category is underweight.\n",
" if metric_bmi < 18.5:\n",
" print('Your category according to the WHO (World Health Organization) is: Underweight')\n",
" # if the bmi is bigger or equal to 18.5 and smaller than or \n",
" # equal to 29.9, the category is pre-obesity.\n",
" elif metric_bmi >= 25 and metric_bmi <= 29.9:\n",
" print('Your category according to the WHO (World Health Organization) is: Pre-obesity')\n",
" # if the bmi is bigger or equal to 30 and smaller than or equal to \n",
" # 34.9, the category is obesity class 1. \n",
" elif metric_bmi >= 30 and metric_bmi <= 34.9:\n",
" print('Your category according to the WHO (World Health Organization) is: Obesity class 1')\n",
" # if the bmi is bigger than or equal to 30 and smaller than \n",
" # or equal to 39.9, the category is obesity class 2.\n",
" elif metric_bmi >= 35 and metric_bmi <= 39.9:\n",
" print('Your category according to the WHO (World Health Organization) is: Obesity class 2')\n",
" # if the bmi is bigger or equal to 40, the category is \n",
" # obesity class 3.\n",
" elif metric_bmi >= 40:\n",
" print('Your category according to the WHO (World Health Organization) is: Obesity class 3')\n",
" # In any other case the category is normal\n",
" else:\n",
" print('Your category according to the WHO (World Health Organization) is: Normal')\n",
"\n",
" # if the user enters 0 in height the program breaks. \n",
" except ZeroDivisionError:\n",
" print('Your height can not be 0. Re-run the program.')\n",
" break\n",
" \n",
"\n",
"# MAIN PROGRAM\n",
"\n",
"# Welcome message for the user. \n",
"# This weird indentation is on purpose so the output is formated nicely.\n",
"print(\"\"\"Welcome to Body Mass Index Caluculation program. \\nThis program will tell you if you have a healthy weight or not. \\nFor more information on Body mass index visit: \n",
"https://www.euro.who.int/en/health-topics/disease-prevention/nutrition/a-healthy-lifestyle/body-mass-index-bmi\n",
"\"\"\")\n",
"\n",
"# A delay is used to prevent the input from being printed before the \n",
"# welcoming message.\n",
"time.sleep(1)\n",
"\n",
"# The main function of the program.\n",
"def main_function():\n",
" # A condition is set to True.\n",
" condition = True\n",
" # While this condition is equal to True: run the following if statement.\n",
" while condition == True:\n",
" # The question for the user is determined. \n",
" question = \"Which system of measurement would you like to use? \\nType METRIC for the metric system and to use kilograms and meters. \\nType IMPERIAL for the imperial system and use pounds and inches. \\nPress ENTER when done.\\n\" \n",
" # Give the user the choice between the metric or imperial system.\n",
" choice = input(question)\n",
" # if the choice is imperial, the imperial() function is run.\n",
" # .lower() is used to make sure that Imperial and IMPERIAL are\n",
" # also accepted. \n",
" # .strip() is used to remove leading and trailing spaces. \n",
" # So that ' imperial' or 'imperial ' is also accepted.\n",
" if choice.lower().strip() == 'imperial':\n",
" print('\\nBody mass index using the imperial system.')\n",
" condition = False\n",
" imperial()\n",
" # if the choice is metric, the metric() function is run. \n",
" # .lower() is used to make sure that Metric and METRIC are\n",
" # also accepted.\n",
" # .strip() is used to remove leading and trailing spaces. \n",
" # So that ' imperial' or 'imperial ' is also accepted.\n",
" elif choice.lower().strip() == 'metric':\n",
" print('\\nBody mass index using the metric system.')\n",
" condition = False\n",
" metric()\n",
" # if there is any other input, the user is reminded to\n",
" # only input imperial or metric.\n",
" else:\n",
" print('\\nMake a choice between IMPERIAL or METRIC without spaces.')\n",
" \n",
"main_function()\n",
"\n",
"# This main_loop function asks the user at the end of the program\n",
"# if he/she would like to run the program again. \n",
"def main_loop():\n",
" # The condition for the main loop is set to true.\n",
" main_loop = True\n",
" # while the condition is true.\n",
" while main_loop == True:\n",
" # Ask the user yes or no.\n",
" answer = input('Would you like to run the program again? Answer with YES or NO: ')\n",
" # If the answer (set in lower letters and stripped from any spaces)\n",
" # is equal to yes, then run the main_function again.\n",
" if answer.lower().strip() == 'yes': \n",
" main_funtion()\n",
" # if the answer (set in lower letters and stripped from any spaces)\n",
" # is equal to no, then set the condition to False \n",
" # to stop the loop from running.\n",
" elif answer.lower().strip() == 'no':\n",
" main_loop = False \n",
" else: \n",
" print('Enter YES or NO please.')\n",
"\n",
"main_loop()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d594d5e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}