Skip to content Skip to sidebar Skip to footer

How To Fix 'string Index Out Of Range' Error In A Function That Ciphers A String

I don't know how to efficiently go back to the beginning of the alphabet, when (index of a letter + 13) is out of range I've written a function that only works if the (index of

Solution 1:

Use the modulo operator %:

Modulo devides the number by the given factor and keeps the rest, eg:

27 % 26 = 1

In your case it would be this two lines:

string.ascii_uppercase[(string.ascii_uppercase.index(i) + 13) % 26]

string.ascii_lowercase[(string.ascii_uppercase.index(i) + 13) % 26]

Post a Comment for "How To Fix 'string Index Out Of Range' Error In A Function That Ciphers A String"