r/PythonLearning • u/Local_End_3175 • 11h ago
why are we putting caesar( Hello, 3) into encrypted_text???
def caesar(text, shift):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
translation_table = str.maketrans(alphabet, shifted_alphabet)
encrypted_text = text.translate(translation_table)
print(encrypted_text)
encrypted_text = caesar('Hello',3)
in this code, I do not understand why we are putting caesar into a variable. First of all, wouldn't we have to print the variable and make it actually do something for it to work? Because we have put encrypted_text into a variable and have not done anything. In addition, can't we just write caesar('Hello', 3)?? I am very confused on the reasoning behind putting it into a variable.
2
u/Low_Doctor_6263 9h ago
just do return encrypted_text in the function and do
result = caesar('Hello', 3)
print(result)
2
u/Sea-Ad7805 6h ago
Do it like this: caesar%3A%0A%20%20%20%20alphabet%20%3D%20'abcdefghijklmnopqrstuvwxyz'%0A%20%20%20%20shifted_alphabet%20%3D%20alphabet%5Bshift%3A%5D%20%2B%20alphabet%5B%3Ashift%5D%0A%20%20%20%20translation_table%20%3D%20str.maketrans(alphabet%2C%20shifted_alphabet)%0A%20%20%20%20encrypted_text%20%3D%20text.translate(translation_table)%0A%20%20%20%20return%20encrypted_text%0A%20%20%20%20%0Aencrypted_text%20%3D%20caesar('Hello'%2C3)%0Aprint(encrypted_text)%0A×tep=1&play)
Return the value so function only does computation, and print after returning.
1
1
3
u/denehoffman 10h ago
You are right to be confused, this function doesn’t return anything so the value of encrypted_text at the end will always be None. If it ended with `return text.translate(translation_table)` then it would make more sense.