+ 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
12 Respuestas
+ 9
It's not necessary to use round()
BMI = float( (wt)/(ht**2))
+ 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
"""
+ 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
+ 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")
+ 1
+ 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 ;)
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
0
You're welcome ch Murali
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
0
What is ternary operator ? 😅 The future is now thanks to science[INACTIVE]
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.