-1

I want to make the number begin from zero if it is more than twenty-five. For example, x = 24 + 10 must be 8. How can I do that?

  • 3
    You mean `(24 + 10) % 26` ? – khelwood Jul 22 '21 at 23:21
  • 1
    Please provide a minimum reproducible example and show what you've tried so far. https://stackoverflow.com/help/minimal-reproducible-example Also, I would assume you are looking for something as simple as `24+10-25` but that would be 9. – ᴓᴓᴓ Jul 22 '21 at 23:22
  • @talfreds They want it to start at 0 when it's 26, not 25. – Barmar Jul 22 '21 at 23:22
  • 1
    Right;. Go look up the "modulo" concept. – Tim Roberts Jul 22 '21 at 23:22
  • @khelwood This question is Python, not Java. – Barmar Jul 22 '21 at 23:23
  • 1
    [How to calculate a mod b in Python?](https://stackoverflow.com/q/991027/3890632) – khelwood Jul 22 '21 at 23:24
  • Or [Find the division remainder of a number](https://stackoverflow.com/q/5584586/3890632) – khelwood Jul 22 '21 at 23:25
  • @barmar You're right, that's what OP means. My logic for -25 is that if it starts at 0 after 25, subtracting 26 would be starting at 1 after 25 – ᴓᴓᴓ Jul 22 '21 at 23:25
  • I want it like whenever you plus some amount of number, it has to be no more than 25, it should begin from zero when it reaches 25. when it is 26, it must be equal to 0 – Diazman Jul 22 '21 at 23:28
  • Yes, exactly. But the number 26 must be equal to 0 and the number of 27 must be equal to 1 etc. But if I add bigger numbers, for example, 24 + 5, the result must be 3. But if I use that if statement you wrote, it makes it equal to 0 – Diazman Jul 22 '21 at 23:39

1 Answers1

0

You can use the modulus operator, or %. a % b finds the remainder when you divide a by b. If you want to keep the number at 25 or lower, you should take the mod with 26.

def add_and_wrap(n1, n2, wrap_at):
    return (n1 + n2) % (wrap_at + 1)