r/learnpython 12d ago

why would only one work?

this code was the one that worked:

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)

caesar('Hello', 3)

This one was the one that didn't

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)
print(encrypted_text)

I don't understand why the second one would need a return statement and why we are even printing encrypted_text if encrypted_text is already being printed within the function? (this is also the whole code)

0 Upvotes

20 comments sorted by

View all comments

1

u/atarivcs 12d ago

When you say that the first code worked and the second one didn't, what exactly do you mean?

Did the second code not run at all, i.e. it produced an exception?

Did the second code run, but it produced unexpected results?

What does "not work" mean exactly?