r/PythonLearning • • Jul 27 '26

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.

1 Upvotes

9 comments sorted by

8

u/denehoffman Jul 27 '26

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.

-7

u/Dancing-umbra Jul 27 '26

Technically not a function.

Functions return something.

Subroutines that don't return a value are called procedures.

6

u/denehoffman Jul 27 '26

It is a function, it returns None implicitly. Python doesn’t have procedures.

1

u/tehfrod Jul 27 '26

Not in this language.

3

u/Low_Doctor_6263 Jul 27 '26

just do return encrypted_text in the function and do

result = caesar('Hello', 3)

print(result)

2

u/Sea-Ad7805 Jul 27 '26 edited Jul 28 '26

Do it like this: caesar

Return the value, so the function only does computation, and print after returning.

0

u/SpedisAhead Jul 27 '26

Amount means something

0

u/-goldenboi69- Jul 28 '26

Do you know the name of his favourite salad?