+ 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

16th Jul 2020, 3:42 PM
KSI šŸ¤”
KSI šŸ¤” - avatar
14 Answers
+ 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
16th Jul 2020, 11:14 PM
Vijay Gunwant
Vijay Gunwant - avatar
+ 1
KSI šŸ¤” Elobarate your question, what's wrong in code?? It giving output "1" on input "0.01"
16th Jul 2020, 4:02 PM
AKSHAYšŸ‡®šŸ‡³
AKSHAYšŸ‡®šŸ‡³ - avatar
+ 1
Thank you Mr. Vijay! I will multiply them by 100 and use whole numbers in my calculation but keep the code the same.
16th Jul 2020, 11:18 PM
KSI šŸ¤”
KSI šŸ¤” - avatar
+ 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)
16th Jul 2020, 11:19 PM
Vijay Gunwant
Vijay Gunwant - avatar
+ 1
Noted
16th Jul 2020, 11:20 PM
KSI šŸ¤”
KSI šŸ¤” - avatar
+ 1
ok
16th Jul 2020, 11:20 PM
Vijay Gunwant
Vijay Gunwant - avatar
+ 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.
18th Jul 2020, 8:20 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
0
I think you need an 'else' function in there
16th Jul 2020, 3:49 PM
MatanzaBC
MatanzaBC - avatar
0
1.23
16th Jul 2020, 5:08 PM
KSI šŸ¤”
KSI šŸ¤” - avatar
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?
16th Jul 2020, 6:51 PM
KSI šŸ¤”
KSI šŸ¤” - avatar
0
Why so, I dont get it? I doesnt substract 0.01 from 0.01 making it zero and breaking the loop
16th Jul 2020, 7:30 PM
KSI šŸ¤”
KSI šŸ¤” - avatar