![]() It is a person’s weight in kilograms divided by the square of his/her height in meters. PrintBMIMessage(bodyMassIndex, 'You are very severely obese.Body Mass Index, or BMI, is a key index that relates to one’s height and weight. Print('Your BMI = ' + str(bodyMassIndex) + ' ' + message) Well, you could refactor the print part and extract it to a function like this: def printBMIMessage(bodyMassIndex, message): # List of pairs (higher-limit, name) sorted In our case, the get_category function does the same thing for all categories: check the if we are under a given limit and if so, return the category name.ĭisclaimer: Next part works under the assumption that body_mass_index < 15 should actually use <= like the other cases. This may be a bit overkill here but sometimes a lot of code can be replaced by a small amount of code working on a properly filled data structure. '.format(body_mass_index, category))Īlso, in Python 3.6, yet another soution was added: F-strings. Or the newer technique: print('Your BMI =. You could write: print('Your BMI = %f You are %s.' % (body_mass_index, category)) You can refer to for documentations and examples. Python offers many tools to format strings so that you do not need to use string concatenations. Going further, a few more details could be improved. Height_in_meter = convert_feet_to_meter(height_in_feet)īody_mass_index = get_body_mass_index(height_in_meter, weight_in_kilo) Height_in_feet = float(input('Enter your height in feet:')) Weight_in_kilo = float(input('Enter your weight in kilo:')) Return weight_in_kilo / (height_in_meter ** 2) Return height_in_feet * INCHES_PER_FOOT * METERS_PER_INCHĭef get_body_mass_index(height_in_meter, weight_in_kilo): This allows the interactivity to be triggered as needed.Īt this stage, we have: INCHES_PER_FOOT = 12ĭef convert_feet_to_meter(height_in_feet): As pointed out by Solomon Ucko, it's generally a good idea to have this performed via a main() function and have the last two (non-blank) lines be if _name_ = '_main_': main(). Thus, the usual strategy is the following: split your code into 2 parts:Ĭode defining functions/constants/classes/etc but without any side-effect or user interactionsĬode actually doing things (input/output, etc) behind an if _name_ = "_main_": guard. Currently, if we do so, we get stuck into the parts asking for user inputs. It highlights how to solve problems you may not be interested in yet but it is a good chance to do things properly.Īlso, if we want to be able to actually reuse your functions, we want to be able to import the file. In our case, defining functions could be a nice touch.ĭisclaimer: Next paragraph can be a bit overwhelming for a beginner, do not worry if you do not fully get it. To make the code easier to understand (and easier to reuse, to test, etc), it is a good habit to split in into smaller reusable chunks. Similarly, half the checks have no effect. Print('Your BMI = ' + str(body_mass_index) + ' You are ' + category + '.')īecause of the way we check body_mass_index, if it is under 15, we get into the first case so there is no need to check elif 15 <= body_mass_index in the else part. This is very specific to Python but instead of body_mass_index > 30 and body_mass_index 40: In your case, we could store the body category in a variable and only print the ouput from a single place: One of the principles of software programming is Don't Repeat Yourself (also written DRY). Having duplicated code makes the code more tedious to read and harder to maintain (if you need to change something, you'll need to change it in many places). This could be used at the beginning of the function as well.Ī lot of code looks like the same line with minimal variations. variables should follow the snake_case naming convention (instead of camelCase)Īs you've noticed for the end of the function, the input builtin takes an optional prompt parameter.blank lines between the different branches of if are more an inconvenience from my point of view.In your case, a few things could be improved regarding style: If you begin with Python, I highly recommend reading it every now and then and trying to apply it. ![]() Python has a Code Style guide called PEP 8. This kind of programming exercice, despite its apparent simplicity, is a good opportunity to learn various things. Print('Your BMI = ' + str(bodyMassIndex) + ' You are very severely obese.') If bodyMassIndex = 15 and bodyMassIndex 16 and bodyMassIndex 18.5 and bodyMassIndex 25 and bodyMassIndex 30 and bodyMassIndex 35 and bodyMassIndex 40: HeightInMeter = heightInFeet * 12 * 0.025īodyMassIndex = weightInKilo / (heightInMeter ** 2) This is the code below: print('Enter your weight in kilo:') I wrote a BMI calculator and I am wondering if there are any improvements I could make to the code.
0 Comments
Leave a Reply. |