-1

Trying to write a python code to encrypt a string.

Encrypts the string and output is an encrypted string.

print "Enter the string "
a=raw_input()
e=''
i=0
while i<len(a):
  c=''
  c+=a[i]
  f=ord(c)
  if i%3==0:
    if f>21:
      e+=chr(f-21)
    else:
      e+=chr(f+5)
  elif i%3==1:
    if ord(c)>24:
      e+=chr(f-24)
    else:  
      e+=chr(f+2)   
  else:
    if ord(c)>21:
      e+=chr(f-20)
    else:
      e+=chr(f+6)     


  i=i+1
  del c

print e   

But when on running this script, error comes.

e+=chr(f-24)
           ^
IndentationError: unindent does not match any outer indentation level   
Shubham Chahal
  • 774
  • 1
  • 5
  • 7

2 Answers2

0

This indentation error is due to the mixing of tabs and spaces in your script. The fix is to go through each line in this file, making sure to use four spaces for each level of indentation. It looks like you are only using two spaces currently, so I'm not exactly sure how you've ended up in this position, but removing all indentation and using four spaces instead of tabs should resolve your problem.

Try to make your code look more like this and see if you still have these issues:

while i<len(a):
    c=''
    c+=a[i]
    f=ord(c)
    if i%3==0:
        if f>21:

Notice how there are four spaces instead of two for each level of indentation. This means the line c='' is four spaces to the right of the while statement. Additionally, the if f>21 line is four spaces to the right of if i%3==0, and eight spaces to the right of the while statement, since it's two levels of indentation under the while statement.

zondo
  • 19,901
  • 8
  • 44
  • 83
curran
  • 198
  • 1
  • 4
  • 14
0

I cleaned up your code:

plaintext = raw_input("Enter the string ")
encrypted = ''
for index, char in enumerate(plaintext):
    char_code = ord(char)
    index_mod = index % 3
    if index_mod == 0:
        if char_code > 21:
            offset = -21
        else:
            offset = 5
    elif index_mod == 1:
        if char_code > 24:
            offset = -24
        else:
            offset = 2
    else:
        if char_code > 21:
            offset = -20
        else:
            offset = 6
    encrypted += chr(char_code + offset)

print encrypted

For fun, it could also be done like this:

offsets = [{True: -21, False: 5}, {True: -24, False: 2}, {True: -20, False: 6}]
upper_limits = [21, 24, 21]

plaintext = raw_input("Enter the string ")
encrypted = ''
for index, char in enumerate(plaintext):
    char_code = ord(char)
    index_mod = index % 3
    offset = offsets[index_mod][char_code > upper_limits[index_mod]]
    encrypted += chr(char_code + offset)

print encrypted

You could even have

offsets = [[5, -21], [2, -24], [6, -20]]

but it's less clear what's going on there.

However now that I see the pattern in the offsets (the second always being the first minus 26) that should be done in code:

offsets = [5, 2, 6]
upper_limits = [21, 24, 21]

plaintext = raw_input("Enter the string ")
encrypted = ''
for index, char in enumerate(plaintext):
    char_code = ord(char)
    index_mod = index % 3
    offset = offsets[index_mod]
    if char_code > upper_limits[index_mod]:
        offset -= 26
    encrypted += chr(char_code + offset)

print encrypted
Alex Hall
  • 34,833
  • 5
  • 57
  • 89