I made this little BMI calculator. Training the if, elif and else. Trying to round off the answer at one digit behind the dot.
But somehow I dont get this thing working.
this is my code:
person = (input('Are (M)ale or (F)emale: '))
weight = float(input('What is youre weight (kg)?: '))
height = float(input('What is youre height (m)?: '))
BMI = weight/(height**2)
if person == "M" and BMI > 25:
print(f'Youre BMI is {BMI}, youre to heavy. Eat less')
elif person == "M" and BMI < 18:
print (f'Youre BMI is {BMI}, youre need to gain weight. Eat more healthy food')
elif person == "F" and BMI >20:
print(f'Youre BMI is {BMI}, youre to heavy. Eat less')
elif person == "F" and BMI < 16:
print (f'Youre BMI is {BMI}, youre need to gain weight. Eat more healthy food')
else:
print (f'Youre BMI is {BMI}, youre weight is perfect.')
round (BMI, 1)
The preferred way to limit the number of digits is at the output formatting as Rob has shown.
âŚbut to understand what you have tried:
round (BMI, 1)
There are two problems preventing the code doing what you wanted.
Python (as well as most of the mainstream programming languages) executes the statements sequentially in the order they are written.[1] You put the statement after all your output statements (print()) so it could not have changed the result of the print() calls.
round (BMI, 1) is an expression which creates a new value as a result. It has no side-effects so if you do not use the result for example by assigning it to a variable: BMI_rounded = round(BMI, 1) then it has no effect in your program.
Also I would suggest limiting the code duplication. For example the repeated messages:
bmi_msg = f'Your BMI is {BMI:0.1f}'
and you can try to unify the âtoo heavyâ and âneed to gain weightâ branches for both male and female by using a more complex expressions in if / elif.
For simplicity lets forget about the control flow statements. âŠď¸
Youre right. My first language is Dutch.
I was a nurse for a long time. But that didnt work out anymore for me.
So now I am waiting to start with a course to become a Data Analist.