0

Code for multiple generator not working

Im a beginner at python and i thought that id write a code which writes the multiples of numbers from some point to another x = float(input()) # From which number y = float(input()) #End number z = float(input()) # Multiple while x<= y: if x%z != 0: x= x+(1/100) continue if x%z == 0: print(x) x= x+(1/100) print ("done") However, for the x+=0.01 , it does not seem to add a 0.01 but instead seems to be adding a 0.010000000001 yet i do not have any reason to see why. I would appreciate any help on the matter

27th Oct 2019, 3:25 PM
Aadit Kumar
Aadit Kumar - avatar
2 Réponses
+ 1
Decimals are hard to store for computers so it stores them as a very close approximation. To get around this you can use the round() method. i.e. x = round(x+(1/100), 2) #keeping it to two decimal places.
27th Oct 2019, 4:29 PM
Russ
Russ - avatar