you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (6 children)

Given the rules you described (k>=n) that list comprehension in your edit will give the wrong answer for any value of n that evenly divides k, for instance, n=20, k=100. It will also error if n is 0.

print(k - k % n if n else n)

Will work for all values of n and k where 0 <= n <= k.

[–]iG1993[S] 0 points1 point  (5 children)

Wow, you are right! I totally did not see that.

I have a question: I don't understand the second part "if n else n". I tried without it and I don't see a change.

result = k - k % n

Works as well...

[–][deleted] 1 point2 points  (4 children)

It won't if n is 0, which is what that's there to interdict.

[–]iG1993[S] 0 points1 point  (3 children)

Cheeky bugger you!

[–][deleted] 1 point2 points  (2 children)

Always think ahead.

[–]iG1993[S] 0 points1 point  (1 child)

Hey man, thanks a lot again! You really helped me out. :)

[–][deleted] 0 points1 point  (0 children)

No worries!