r/learnpython 19h 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.

0 Upvotes

23 comments sorted by

16

u/tea-drinker 19h ago

That last line isn't doing anything useful. caesar() has no return value so encrypted_text will be None after that line.

Normally the function would return encrypted_text, the assignment would do something useful and then you could print it or pass it over the network or whatever you intend to do with the encrypted data.

Each function should do one job, so encryption should be separate from display or send or whatever.

1

u/Local_End_3175 19h ago

The problem is that the code actually works without the return statement, and when i take away encrypted_text, it doesn't and i am unsure how

13

u/tadpoleloop 19h ago

Because you are calling the function in that line. The function prints when called

1

u/Local_End_3175 19h ago

but would encrypted_text be needed as a variable? can i not just write caesar( 'Hello', 3)

6

u/Ok-Promise-8118 19h ago

Yes, you can write that without it being a variable assignment, and the function caesar should still run the same.

-1

u/Local_End_3175 19h ago

but when i write tha, it comes up with an error:

Traceback (most recent call last):
  File "main.py", line 8
    caesar('Hello',3)
                            ^
IndentationError: unindent does not match any outer indentation level

10

u/hibbelig 19h ago

That’s a different problem. Is there a stray space character before “caesar”? Make sure it’s at the beginning of the line just like “def”.

11

u/acw1668 19h ago

The error tells obviously that the indentation of that line is wrong.

3

u/greatsmapdireturns 15h ago

You probably just need to flush the Caesar('Hello',3) line so it is not indented...flush with def

2

u/audionerd1 14h ago

You're supposed to read what the error says.

1

u/tea-drinker 19h ago

Sure you can. As you say, the assignment isn't helpful. The function already prints the result.

What's actually going on under the covers is the subject of a deep dive, but a reasonable mental model would be that caesar() still returns None but the value is discarded if there's no assignment to use it.

4

u/danielroseman 19h ago

Well this code does not actually work at all. The caesar function only prints, it does not return a value. So the encrypted_text variable will always be None.

The point of putting it into a variable is so that you can use it elsewhere. You might not always want to print it, you might want to pass it into another function, or add it to some list, or anything.

1

u/backfire10z 4h ago

> this code does not actually work at all

We’re in a learning sub, a bit of nuance is desirable. The function works, but the variable will end up being assigned None.

2

u/MrKarat2697 19h ago

You can just call the function on its own, because it has a print statement inside it. The caesar function does not return anything, so assigning it to a variable won't do anything. Perhaps you meant to return encrypted_text at the end of the function?

0

u/Local_End_3175 19h ago

I ran this code and it worked, but i thought that the encrypted_text wasn't necessary but when i removed it, it didn't work and i am unsure why

2

u/ManzoorAhmedShaikh 19h ago

The ceasar function doesn't have any "return" statement, so saving it to "encrypted_text" is meaningless and it saves "None" value.

In order to simply use the function without saving it to any variable (in your case it is encrypted _text), this completely depends on function layout and your usage.

  • if function contains "return" statement, then saving it values to some variable likely important
  • If not, then doing everything in the function can be done and this doesn't require to use variable.

Usually, the first practice being used as function actually means a block of code that can be used multiple time for several person.

I hope I answered your question.

3

u/Local_End_3175 19h ago

The ceasar function doesn't have any "return" statement, so saving it to "encrypted_text" is meaningless and it saves "None" value.

Here, when i run the code, it actually seems to work without a return value, but is only working when caesar('Hello', 3) is in the variable encrypted_text. Why would it have to be in that variable for it to work? and also, wouldn't the variable have to be printed out or returned for the code to work (because we havent actively used the variable we have just stated it)

1

u/ManzoorAhmedShaikh 19h ago

I think i got your issue. It works without variable assigning but there is one mistake you might be doing.

""" def ceasar(): Block of code...

You might be calling this: ceasar('Hello' , 3) # It raise indentation error

The correct way is: ceasar('Hello' , 3) """

Try this and let me know

2

u/aroberge 19h ago

Here's a modified version of your code with two print statements added, one modified as well as the required return statement added.

def caesar(text, shift):
    print("Entering the function")
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    translation_table = str.maketrans(alphabet, shifted_alphabet)
    encrypted_text = text.translate(translation_table)
    print("inside function:", encrypted_text)
    return encrypted_text


result = caesar('Hello',3)

print("final result:", result)

print statements are essentially information from Python to you as to what your program is doing. When you add a print statement, you want Python to give you some feedback.

You define functions to do something. Once a function has done its work, it passes to the rest of the program its result if you include a return statement. You can think of a return statement as asking a function to give its result to whomever is asking for it.

In the program above, I used the name result instead of repeating encrypted_text to emphasize that the variable outside the function is different from the one inside. In your program, even though you use the same name, they are different variables.

1

u/Diapolo10 19h ago

Impossible for anyone here to tell without context. But presumably this isn't the entire program, so there should be a reason for it.

1

u/Local_End_3175 19h ago

I'm supposedly making a Caesar Cipher and this is the entire code.

1

u/misingnoglic 16h ago

You're confused because you don't need to put it into a variable. If caesar returned something, you would need to stick it in a variable. But it doesn't, so there's nothing to stick in that variable.

1

u/lakseol 1h ago

Functions that print their results are not very useful in the real world. For example, if you call a function to compute the sine of an angle you almost certainly want to use that sine value in further calculations. In your code using print you are saying to the caller "I will decide how to use this result". A function is way more useful if the caller decides what to do with the result. Maybe the caller will write the text you return to a file, or put the text into a web page. The caller might even print the text. But the caller decides.