 # Python operator %

Why do we use % in this exercise?
in Step1:
new_proyect += alphabet[(letter_value+14)%26]

1 Like

The `%` is the modulo operator, it is different than the `/` operator because the `/` divides it normally while the `%` operator returns the remainder, if there isn’t any remainder then it will return 0.
Here’s an example:

``````print(5 / 2) # prints 2.5
print(5 % 2) # prints 1
print(4 % 2) # prints 0
``````

Unfortunately I’m not pro so I couldn’t view the exercise but I hope I helped with this % can be used to perform “clock addition.” Say you have a 12-hour clock, (zero on top for the simplest example - we all do zero-based counting here, right?)

If it’s 3:00 and we add 3 hours, that makes its 6:00, agreed?

What if it’s 3:00 and we add 17 hours? Well, that’s 20:00, which we can’t deal with, but

`20 % 12 = 8` … and if you draw a clock, start at 3 and count around 17 hours, you’ll get to 8.

Start where you are on the clock, add any number of hours, “mod” the sum by 12, and you’ll get the correct hour. **

So, to the point: Just substitute the letters of the alphabet for the numbers on a 26-hour “clock”, and you can see how % can be used to give you a simple encryption system, the so-called Caesar Cipher.

** (Don’t look now, because it might make you crazy, but Python’s mysterious way of modding negative numbers enables you to even move the hands backwards and still wind up at the correct hour!)

1 Like

I understand this modulo operator but I do not understand why it is used in this exercise.

``````This message has an offset of 10.  You have to decode it.
``````

alphabet = “abcdefghijklmnopqrstuvwxyz”
punctuation = ".,?’! "
message = “xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!”
translated_message = “”
for letter in message:
if not letter in punctuation:
letter_value = alphabet.find(letter)
translated_message += alphabet[(letter_value + 10) % 26]
else:
translated_message += letter
print(translated_message)

Thank you

1 Like

Thank you. Now I understand the exercise

1 Like