+ 1

Why Python division it's so bad?

The regular division operator on python (/) its very bad, sometimes just gives wrong values. I was doing the test for the 4th chapter of Python for beginners and was stuck like half an hour because I was thinking that it was something wrong with my code but it wasn't, and then just tried to change "/" for "//" and it worked just fine đŸ€ŠđŸ»â€â™‚ïžđŸ€ŠđŸ»â€â™‚ïžđŸ€ŠđŸ»â€â™‚ïžđŸ€ŠđŸ»â€â™‚ïž

25th Feb 2023, 8:14 PM
Juan Manuel GonzĂĄlez Vega
Juan Manuel GonzĂĄlez Vega - avatar
10 Respostas
+ 10
Juan Manuel GonzĂĄlez Vega if you post a link to your code and a proper task description we can see what issue you have, and we may help you.
26th Feb 2023, 10:21 AM
Lothar
Lothar - avatar
+ 5
/ and // are different. Read the lesson again and check comments to understand it better.
25th Feb 2023, 11:12 PM
Lamron
Lamron - avatar
+ 5
Your code should look like this: if imc < 18.5: print("Underweight") elif imc < 25: print("Normal") elif imc < 30: print("Overweight") else: print("Obesity") Why the 24.9 and the 29.9? Why not 25 and 30?
27th Feb 2023, 5:56 PM
Paul
Paul - avatar
+ 5
It is strange that it is different, I have seen more people using 24.9 and 29.9, but I couldn't understand why. Thanks for showing it. I think the English version is more logical.
1st Mar 2023, 5:27 AM
Paul
Paul - avatar
+ 4
Might be there u need to print output in integer form not floating point number. So read input output formats and constraints
25th Feb 2023, 9:07 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
This is the exercise text. I don't see 24.9 nor 29.9. The resulting number indicates one of the following categories: Underweight = less than 18.5 Normal = more or equal to 18.5 and less than 25 Overweight = more or equal to 25 and less than 30 Obesity = 30 or more
28th Feb 2023, 5:55 AM
Paul
Paul - avatar
+ 2
peso = input() altura = input() try: imc = float(peso) / (float(altura) **2) except: print("Invalid Input") if imc < 18.5: print("Underweight") elif imc < 24.9: print("Normal") elif imc < 29.9: print("Overweight") elif imc >= 30: print("Obesity") #That was my code, when I use // instead of / on the line 4 it works just fine
26th Feb 2023, 2:45 PM
Juan Manuel GonzĂĄlez Vega
Juan Manuel GonzĂĄlez Vega - avatar
+ 2
Well mine looks different, that's weird, it's in Spanish but you can translate it: El nĂșmero resultante indica una de las siguientes categorĂ­as: Underweight = menos de 18.5 Normal = 18.5 - 24.9 Overweight = 25 - 29.9 Obesity = 30 o mĂĄs That's what it says đŸ€·đŸ»â€â™‚ïž
28th Feb 2023, 9:26 PM
Juan Manuel GonzĂĄlez Vega
Juan Manuel GonzĂĄlez Vega - avatar
+ 2
Yeah true English seems more logical 😅
1st Mar 2023, 6:47 PM
Juan Manuel GonzĂĄlez Vega
Juan Manuel GonzĂĄlez Vega - avatar
0
The numbers are like that because that's what the exercise said, I just do it the way they were asking đŸ€·đŸ»â€â™‚ïž
28th Feb 2023, 3:49 AM
Juan Manuel GonzĂĄlez Vega
Juan Manuel GonzĂĄlez Vega - avatar