0

Can someone explain rot13 in python?

It's bugging me for days.. def rot_13(): encoding={} abc='abcdefghijklmnopqrstuvwxyzabcdefghijklm' ABC='ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM' for i in range(0,26): encoding.update({abc[i] : abc[i+13]}) encoding.update({ABC[i] : ABC[i+13]}) return encoding print(rot_13()) def decode(s): key = rot_13() sentence = [] for i in range(0, len(s)): if key.get(s[i], 'none') != 'none': sentence.append(key[s[i]]) else: sentence.append(s[i]) new_sentence = ''.join(sentence) return new_sentence print(rot_13('V NZ YRNEAVAT CLGUBA JVGU FUR PBQRF NPNQRZL!')) https://code.sololearn.com/c3rA9QJWSEse/?ref=app https://code.sololearn.com/c3rA9QJWSEse/?ref=app

16th Oct 2022, 1:06 PM
Liat123
Liat123 - avatar
9 odpowiedzi
+ 1
NOT print(decode(rot_13('V NZ YRNEAVAT CLGUBA JVGU FUR PBQRF NPNQRZL!'))) BUT INSTEAD print(decode('V NZ YRNEAVAT CLGUBA JVGU FUR PBQRF NPNQRZL!'))
16th Oct 2022, 8:38 PM
Lisa
Lisa - avatar
+ 4
Which part would you like us to explain? People are more likely to inspect your code if you put it in a script on sololearn playground instead of pasting it into the description: – Go to Code section, click +, select the programming language, insert your code, save. – Come back to this thread, click +, Insert Code, sort for My Code Bits, select your code. This way, it is easier to read and everyone can test it! ROT13: https://en.m.wikipedia.org/wiki/ROT13
16th Oct 2022, 2:33 PM
Lisa
Lisa - avatar
+ 3
Hi, Liat123 ! I made a demo to explain rot13 for you. You can look at it here: https://code.sololearn.com/czp5Fq09D4N4/?ref=app
16th Oct 2022, 5:39 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
#The first function could be omitted entirely. #r13 does encoding and decoding by offsetting the letters by 13 chars abc='abcdefghijklmnopqrstuvwxyzabcdefghijklm' ABC='ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM' encoding = {} #<--This is the key✔️ #create the encoding dictionary for i in range(0,26): encoding.update({abc[i] : abc[i+13]}) encoding.update({ABC[i] : ABC[i+13]}) #print(encoding) #encoder/decoder def r13(s): key = encoding #<--key used here👈 sentence = [] for i in range(0, len(s)): if key.get(s[i], 'none') != 'none': sentence.append(key[s[i]]) else: sentence.append(s[i]) new_sentence = ''.join(sentence) return new_sentence print(r13('I AM LEARNING PYTHON WITH SHE CODES ACADEMY!')) print(r13('V NZ YRNEAVAT CLGUBA JVGU FUR PBQRF NPNQRZL!')) print(r13('it is a simple Encoding trick')) print(r13('vg vf n fvzcyr Rapbqvat gevpx'))
16th Oct 2022, 10:05 PM
Bob_Li
Bob_Li - avatar
+ 1
It looks like a function that takes whatever letter from the "encoded message" and adds 13 to it to output the correct "decoded message".
16th Oct 2022, 2:20 PM
Ausgrindtube
Ausgrindtube - avatar
0
Thanks everyone, I inserted the code to the post. I understood the general idea of the question but the last print didn't run. The question has a dictionary and the letters make sense but something seems off.
16th Oct 2022, 8:29 PM
Liat123
Liat123 - avatar
0
rot13() does not take any argument. Could it be that you mean print(decode('V NZ YRNEAVAT CLGUBA JVGU FUR PBQRF NPNQRZL!')) In the last line your code?
16th Oct 2022, 8:31 PM
Lisa
Lisa - avatar
0
Tried that now, still the same error
16th Oct 2022, 8:34 PM
Liat123
Liat123 - avatar
0
Yay it worked! :D thanks for everyone! Is it ok to post in sololearn 5 posts a day? Or can I insert couple of codes in one post? Don't want to spam but could really use some help
16th Oct 2022, 9:04 PM
Liat123
Liat123 - avatar