Home > Enterprise >  getting weird extra numbers on program to count down in python
getting weird extra numbers on program to count down in python

Time:06-29

sorry if the title is weird. I don't know how else to word my problem. I am trying to make a program to start at 2 and count down by 0.1 seconds until zero. this is the code I have so far

import time
starting_int = 2
for i in range(20):
    time.sleep(0.1)
    print(starting_int)
    starting_int -= 0.1

I am getting this result

2
1.9
1.7999999999999998
1.6999999999999997
1.5999999999999996
1.4999999999999996
1.3999999999999995
1.2999999999999994
1.1999999999999993
1.0999999999999992
0.9999999999999992
0.8999999999999992
0.7999999999999993
0.6999999999999993
0.5999999999999993
0.49999999999999933
0.39999999999999936
0.2999999999999994
0.19999999999999937
0.09999999999999937

if it is at all possible to just get the first 2 int. please let me know how I can fix my problem.

CodePudding user response:

I assume you mean to round the figures to 2 decimal places? To do that you can use the round function.

starting_int = round((starting_int-0.1),2)

CodePudding user response:

I think the problem before was that it was an integer not a float and so this should work

import time

starting_int = 20

for i in range(20):
    time.sleep(0.1)
    print(starting_int / 10)
    starting_int -= 1
  • Related