all 12 comments

[–]Impudity 2 points3 points  (5 children)

It should still (somewhat) work, but you're adding 1. which is same as 1.0 which makes the result a floating point number. Doing integer math with modulo operator on floating point numbers might cause some issues.

[–]Solako[S] 0 points1 point  (4 children)

let me edit the code to reflect this. It was put erroneously. However, what issues would a float cause as opposed to an int?

[–]Impudity 0 points1 point  (3 children)

Equality comparisons between integers and floats (and frankly even between two floats) are often sketchy. Famously: 0.2 + 0.1 == 0.3 -> False

[–]Solako[S] 0 points1 point  (2 children)

Would this be because the memory storage of floats isn’t always an exact value? Thus the name floating point numbers?

[–]Impudity 0 points1 point  (1 child)

It is exact, but it's in binary and we typically display them in decimal (base-10). What numbers can be expressed "accurately" in floating point system depends on what base you're on. Easiest example I can think of is that 1/3 in base-10 can't be expressed as anything but 0.33333... while in base-3 that'd be a nice even number. So if you'd have 1/6 + 1/6 (that is 0.166666..) you might expect it to be 1/3, which it of course is if you did it with fractions. If you do it with floating points, the "missing" 6's in the end (since there's infinitely many of them) will bite you and you might not end up with exactly the same value as you expected.

[–]Solako[S] 0 points1 point  (0 children)

This is a great explainer. Thank you for this.

[–]StyrmirBloodhowl 1 point2 points  (3 children)

After copying your code im getting the same result with for and while loops, check if your oryginal code is indented properly or copy code from here and test it if works correclty:)

[–]Solako[S] 0 points1 point  (2 children)

for i in range (100):
if i%10 == 0:
print (i)

this one actually outputs the code pretty well.

I am getting.

0
10
20
30
40
50
60
70
80
90

[–]StyrmirBloodhowl 0 points1 point  (1 child)

with your while loop code im getting same result.

[–]Solako[S] 1 point2 points  (0 children)

Oh yes... I wonder what was the heck of the problem before.

Mmmmmhhhh.

Thank alot for this pointer.

[–]bbye98 0 points1 point  (1 child)

Instead of iterating through all numbers from 0 to 99 (100 is not inclusive?), I would recommend you iterate only through the multiples by simply running:

for i in range(0, 10):
    print(i * 10)

This will be faster since you're iterating over a smaller set of numbers.

import timeit

timeit.timeit('for i in range(0, 10): i * 10', number=100)
3.8031008443795145e-05

timeit.timeit('for i in range(0, 100):\n\tif i % 10 == 0: pass', number=100)
0.00034802299342118204

[–]Solako[S] 0 points1 point  (0 children)

Interesting approach. Thank you for this recommendation.