Why is Syntex error coming out?

+1 vote
asked Aug 16 by Yungsil lee (220 points)

I have entered a source that implements caesar crypto tools. 
Then, why comes syntex error?

Which part is wrong?

And, for example, if I want to encrypt 'AWESOME STUDENTS' 
how do you modify it?

I'm a first-timer of Python. I ask for your warm attention!


import sys

ENC = 0
DEX = 1

def makeDisk(key):
    keytable = map(lambda x: (chr(x+65),x), range26))

    key2index = {}
    for t in keytable:
        alphabet, index = t[0], t[1]
        key2index[alphabet] = index

        if key in key2index:
          k = key2index[key]
        else:
          return None, None

        enc_disk = {}
        dex_disk = {}

        for i in range(26):
          enc_i = (i+k)%26
          enc_ascii = enc_i+65
          encc_disk[chr(i+65)] = chr(enc_ascii)
          dec_disk[chr(enc_ascii)] = chr(i+65)

        return enc_disk, dec_disk


  def caesar(msg, key, mode):
       ret = ' '

       msg = msg.upper()
       enc_disk, dex_disk = makeDisk(key)

      if enc_disk is None:
        return ret

      if mode is ENC:
        disk = enc_disk
      if mode is DEC:
        disk = dec_disk

      for c in msg:
        if c in disk:
          ret += disk[c]
          else:
          ret += c

        return ret


    def main():
        plaintext = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        key = 'F'


        print('Original:\t%s' %plaintext.upper())
        ciphertext = caesar(plaintext, key, ENC)
        print('Caesar Cipher:\t%s' %ciphertext)
        deciphertext = caesar(ciphertext, key, DEC)
        print('Deciphered:\t%s' %deciphertext)

if __name __=='__main__':
    main()


2 Answers

0 votes
answered Aug 20 by Rahul Choubey (480 points)

I'm not sure, but here's a much shorter solution:

def add_caesar_cipher(text, shift):
    import string
    trans_table = str.maketrans(string.ascii_lowercase + string.ascii_uppercase,  string.ascii_lowercase[shift:] + string.ascii_lowercase[:shift] + string.ascii_uppercase[shift:] + string.ascii_uppercase[:shift])
    return text.translate(trans_table)
def remove_caesar_cipher(text, shift):
    return add_caesar_cipher(text, -shift)

EDIT:

I found a couple of mistakes. First, the method caesar() is on the wrong indentation level. Second, add a "(" in between the range and the 26 on line 7/8. Look for more!
commented Aug 20 by Rahul Choubey (480 points)
range26 is not valid. Perhaps range(26)?
0 votes
answered Aug 21 by LiOS (5,120 points)
Python uses indentations to represent code blocks while other languages such as C use 'curly braces' {.

The main issue with your code is the indentations, but this could be caused by you C&P the code over.

I would recommend reviewing the indentations (remember 1 tab or 4 spaces), and then running, writing down the process at each stage in a text document etc.

The code you have also looks overcomplicated for what it does, so you could find a simplier method which will help you.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...