0

I got a question from my lecturer which is to implement an encoder/decoder of ROT-13. I don't really understand the question asked. The question sounds like this

In cryptography, a Caesar cipher is a simple encryption technique in which each letter is replaced by a letter some fixed number positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E and so on. The method is named after Julius Caesar, who used it to communicate with his generals. ROT-13 is a widely used example of a Caesar cipher where the shift is 13. In Python, the key for ROT-13 may be represented by means of the following dictionary:

The question have give the dictionary :

key = {'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s', 'g':'t', 'h':'u', 'i':'v', 'j':'w', 'k':'x', 'l':'y', 'm':'z', 'n':'a', 'o':'b', 'p':'c', 'q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', 'x':'k', 'y':'l', 'z':'m' }

I have to encode/decode this message :

pnrfne pvcure? v zhpu cersre pnrfne fnynq!

This is my coding after I have research from several web site. I couldn't run my program since it has an error. Can anyone help me solve my problem?

def decodeThis(n):
    key = {'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s', 'g':'t', 'h':'u', 'i':'v', 'j':'w', 'k':'x', 'l':'y', 'm':'z', 'n':'a', 'o':'b', 'p':'c', 'q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', 'x':'k', 'y':'l', 'z':'m' }
    for x in n:
        v =x in key.keys()
        if v == True:
            py += (key[x])
        else:
            py += x
    return py

Yes = decodeThis("pnrfne pvcure? v zhpu cersre pnrfne fnynq!")
print(Yes)
N.A
  • 23
  • 1
  • 1
  • 4
  • 1
    What is the error? – Josh Lee Aug 23 '17 at 00:15
  • What do you think `A[n]` is? – OneCricketeer Aug 23 '17 at 00:19
  • @Dekel -- this isn't a duplicate, IMHO. The posting is to find what is wrong with *this* program. The one you cited uses a different method -- although comparable. – Prune Aug 23 '17 at 00:19
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Aug 23 '17 at 00:20
  • @Prune So if thats the case this should be closed/moved to codereview :) – Dekel Aug 23 '17 at 00:20
  • @Asyiqin: you have several basic errors in referencing variables. I suggest that, for now, you use **print** statements to check the values you're using. Make sure you know what each variable does and how it relates to the others. – Prune Aug 23 '17 at 00:21
  • For instance, your **for** loop steps through the characters of **A**, but the **if** condition tries to use **n** as if it were an index, rather than the individual character. – Prune Aug 23 '17 at 00:22
  • @Dekel: I disagree. CodeReview would be to improve working code. This still has syntax and semantic errors, basic programming problems. – Prune Aug 23 '17 at 00:23

0 Answers0