Why does this give an unexpected answer? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does this give an unexpected answer?

def countdown(): i=10 while i > 0: yield i i -= 0.2 for i in countdown(): print(i) #this should count down by 0.2 The result is long decimals why?

25th Mar 2019, 9:08 PM
Adrian
3 Answers
+ 4
So this is not about the inaccuracy of floats? Because they have long decimals sometimes. Try print(0.2) for an instance.
25th Mar 2019, 10:54 PM
HonFu
HonFu - avatar
+ 1
No, I dont' think so. But thanks for answering Michael Williams
25th Mar 2019, 9:42 PM
Adrian
0
Would using round be useful?
25th Mar 2019, 9:22 PM
Michael Williams
Michael Williams - avatar