-2

I want to Print all the numbers between 5 to 8, with jumps of 0.3. I got 2 problems doing it:

  1. It's not very accurate (I know the reason, what I dont know is how to get 5.89999999999 to become 5.9)

  2. I dont know how to do it without any imports and functions (without numpy and without xrange which btw does not exist in python 3.x anymore as I understood)

This is just for a Homework question, I'm a new python learner, so I dont realy know ALL the tools and tricks of python.

max = 9
min = 5
step = 0.3
while min <= max:
    min += step
    print(min)

Expected:
5, 
5.3,
5.6,
5.9,
6.2,
...

Actual reasults:
5.3,
5.6,
5.89999999995,
6.1999999999,
6.4999999999,
...
Jhon Margalit
  • 451
  • 4
  • 12

1 Answers1

0

Just round min using round() before printing it. If you want the original value of the variable to be also printed, you'll want to change the position of the print statement.

while min <= max:
    print(min)
    min+=step
    # rounds min to the first decimal place
    min = round(min,1)

Results:

5
5.3
5.6
5.9
6.2
6.5
6.8
7.1
7.4
7.7
8.0
8.3
8.6
8.9
9.2
Axiumin_
  • 2,107
  • 2
  • 15
  • 24