Coded Correspondence Project help

The solution to the first question is below, asking to decipher the code. I am just wondering why the modulo operator is needed here? If anyone could explain this answer it would help me understand.

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)

The result of doing some_number % 26 would be from 0 to 25 (inclusive).
The length of alphabet is 26, so the largest valid index would be 25,
so we do number % 26 to get a number that would be a valid index (since the result wouldn’t be bigger than 25).

1 Like

So the modulo is used to keep the answer within the intended indices?

Yes. That’s what the % is used for here.