31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
|
import string
|
||
|
|
||
|
|
||
|
def to_encrypt(text, delta):
|
||
|
alphabet = list(string.ascii_lowercase)
|
||
|
new_text = []
|
||
|
for letter in text:
|
||
|
if letter.isalpha():
|
||
|
letter_index = alphabet.index(letter) + delta
|
||
|
if letter_index > len(alphabet):
|
||
|
letter_index = letter_index - len(alphabet)
|
||
|
if letter_index < 0:
|
||
|
letter_index = len(alphabet) + letter_index
|
||
|
new_text.append(alphabet[letter_index])
|
||
|
else:
|
||
|
new_text.append(letter)
|
||
|
return "".join(new_text)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
print("Example:")
|
||
|
print(to_encrypt('abc', 10))
|
||
|
|
||
|
# These "asserts" using only for self-checking and not necessary for auto-testing
|
||
|
assert to_encrypt("a b c", 3) == "d e f"
|
||
|
assert to_encrypt("a b c", -3) == "x y z"
|
||
|
assert to_encrypt("simple text", 16) == "iycfbu junj"
|
||
|
assert to_encrypt("important text", 10) == "swzybdkxd dohd"
|
||
|
assert to_encrypt("state secret", -13) == "fgngr frperg"
|
||
|
print("Coding complete? Click 'Check' to earn cool rewards!")
|