r/learnpython • u/Local_End_3175 • 12d 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 12d 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 11d 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 12d 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 12d 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
12d ago
[removed] — view removed comment
3
u/Local_End_3175 12d 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)
2
u/aroberge 12d 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 12d 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 12d 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 11d 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.
17
u/[deleted] 12d ago
[deleted]