+ 1
Please assist with while loop question
Can someone please explain why the last elif for 0.01 just gets ignored. If numbers are integers all is good but for floats it doesn't work for some reason. https://code.sololearn.com/c4wqlT36SQIm/#py
14 Réponses
+ 2
KSI 🤡
The problem is not in your code but it is how the float calculations are performed . Because in python floats are stored in a different way and the operation performed on them may show unexpected result.
You will get to know you error if you insert a print (amount ) statement in your loop.
Actually when you are performing the comparison operators then avoid comparing flaot values because their are stored in different way and some time give unexpected results.
To understand it try printing 0.1+0.1+0.1 and see the result you will get to know your error
+ 1
KSI 🤡 Elobarate your question, what's wrong in code??
It giving output "1" on input "0.01"
+ 1
Thank you Mr. Vijay!
I will multiply them by 100 and use whole numbers in my calculation but keep the code the same.
+ 1
KSI 🤡
try to convert them to int because even if you multiply it by 100 it will still remain a flaot an day raise exception (after multiplying by 100)
+ 1
Noted
+ 1
ok
+ 1
KSI 🤡 it is not getting ignored. I run the code, input 0.01 and it is giving 1 as output. Which means it is going through elif amount >= 0.01 amount -=0.01 and then again entering the loop and then as amount is zero now,it is breaking.
0
I think you need an 'else' function in there
0
1.23
0
Exactly the problem.
When i type whole numbers, such as 2 for example I get output 1, which is correct. 3 gives 2 and so on.
When I input a float amount such as 1.23 there is no output...
Why is there no output?
0
Why so, I dont get it?
I doesnt substract 0.01 from 0.01 making it zero and breaking the loop