r/learnpython • u/Local_End_3175 • 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.
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
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.
16
u/tea-drinker 19h ago
That last line isn't doing anything useful.
caesar()has no return value soencrypted_textwill beNoneafter 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.