+ 1

In python for beginners there is a task to code a BMI calculator. What a better code

My code is working good but I am confused that whether I can use loops (Flow control concept) here .. Help me in making my code to look more better with your feedback

31st May 2021, 3:21 PM
ch Murali
12 Réponses
+ 9
It's not necessary to use round() BMI = float( (wt)/(ht**2))
31st May 2021, 3:37 PM
Simba
Simba - avatar
+ 7
# much more "pythonic" code: weight = float(input()) height = float(input()) bmi = weight / (height ** 2) if bmi < 18.5: print("Underweight") elif bmi < 25: print("Normal") elif bmi < 30: print("Overweight") else: print("Obesity") """ + meaningfull variable name + spacing between operator and operands + not use of unuseful parenthesis (outer expressions), but explicit parenthesis for exponentiation even if operators precedence make them optional + inlined block of il..elif..else statements since only one statement and total line length is still short enough """
31st May 2021, 8:54 PM
visph
visph - avatar
+ 6
I don't think you need to use a loop since you only have to compare the values and give outputs. most BMI calculators made by people here used only if and else statements
31st May 2021, 3:28 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 3
#my bmi code weight = float(input()) height = float(input()) bmi = weight/height**2 if bmi < 18.5: print("Underweight") elif bmi >= 18.5 and bmi < 25: print("Normal") elif bmi >= 25 and bmi < 30: print("Overweight") else: if bmi > 30: print("Obesity")
1st Jun 2021, 2:07 PM
Talgat Khussainov
Talgat Khussainov - avatar
31st May 2021, 3:31 PM
Eashan Morajkar
Eashan Morajkar - avatar
31st May 2021, 4:22 PM
ch Murali
+ 1
The future is now thanks to science[INACTIVE] python do not have ternary operator, only inlined if..else statement ^^ ch Murali ternary operator is operator wich takes 3 operands... most of languages (but at least not python nor kotlin) have an if..else ternary operator shortcut: if condition: value = v1 # if condition == True else: value = v2 # if condition == False would be shortened to: value = condition ? v1 : v2 but in python you can only do: value = v1 if condition else v2 calling the second "ternary operator" is a language abuse ;P ... and chaining ternary operator (or inlined if..else) to mimic if..elif.else is often considered as bad practice as ends with a code mess, quite unreadable, mostly used by code shortener/obfuscators or in code golf... decreasing maintanability :( finally, python doesn't have such operator by design choice for these reasons, and code recommendations prevent longest lines ;)
1st Jun 2021, 4:18 PM
visph
visph - avatar
0
wt = float(input()) ht = float(input()) BMI = round(float( (wt)/(ht**2) ),2) if BMI < 18.5: print("Underweight") elif 18.5 <= BMI <= 25: print("Normal") elif 25 <= BMI <= 30: print("Overweight") elif BMI > 30: print("Obesity") Here is my code
31st May 2021, 3:21 PM
ch Murali
0
You're welcome ch Murali
31st May 2021, 4:35 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
weight = int(input()) height = float(input()) a = (weight / (height*height)) if a < 18.5: print ("Underweight") if 18.5 <= a < 25: print ("Normal") if 25 <= a < 30: print ("Overweight") if a >= 30: print ("Obesity") This is my code
31st May 2021, 5:51 PM
Yash Soni
Yash Soni - avatar
0
What is ternary operator ? 😅 The future is now thanks to science[INACTIVE]
1st Jun 2021, 2:06 PM
ch Murali
0
@visph thanks for pointing this out. Yes, I agree with you. It is considered a bad practice to use. Also thanks for the information which I didn't know before.
2nd Jun 2021, 7:18 AM
The future is now thanks to science
The future is now thanks to science - avatar