Round function wrong code?

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)

One way, would be to convert the number to a formatted string, then use said string in your output:

BMI_Str = str(f'{BMI:0.1f}')

Also, if you double quote your print() functions, you can punctuate your the output:

e.g: print(f"Your BMI is {BMI_Str}, you're to heavy. Eat less.")

To add…

It’s a small point, but there’s no need for the extra parentheses on the input() function:

person = input('Are (M)ale or (F)emale: ')

… which should be checked for ‘sanity’ (as with all and any user input) but I’m sure you’ll learn all about that, in good time.

Another thought: maybe English is not your first language? If that’s the case, maybe you don’t fully understand the English punctuation.

2 Likes

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.

  1. 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.
  2. 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.


  1. For simplicity lets forget about the control flow statements. ↩︎

2 Likes

Thank you so much for the remarks. I will give it a shot.

Thank you Rob for youre comment. You helped me allot.

1 Like

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.

Well, your English is 100% better than my Dutch (which is zero).

A major change of carer direction for you: the best of luck with it.

1 Like