-1

I'm getting approximate values for start variable in below code:

start = 1
end = 2
incr = 0.2
while start < end:
    print(start)
    start = start + incr

Actual output:
1
1.2
1.4
1.5999999999999999
1.7999999999999998
1.9999999999999998
Hit any key to close this window...

Expected output:
1
1.2
1.4
1.6
1.8

I couldn't figure out why this is happening.

Infinity
  • 165
  • 1
  • 1
  • 7

1 Answers1

0

Try this:

start = 1
end = 2
incr = 0.2
while start < end:
    print(start)
    start = round(start + incr,1)
Shadab Hussain
  • 794
  • 6
  • 24