# 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.")`

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