Ceasar Cipher help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • barks33
    New Member
    • Apr 2010
    • 2

    Ceasar Cipher help

    Hi all, im writing a caesar cipher and ive done most of the program (see below)
    Code:
    def decrypt(ciphertext, shift):
        decrytped_text = " "
    
    
        for letter in ciphertext:
            if letter.isalpha():
                num = ord(letter)
                num += key
    
                if letter.isupper():
                    if num > ord('Z'):
                        num -= 26
                    elif num < ord ('A'):
                        num += 26
                elif letter.islower():
                    if num > ord('z'):
                       num -= 26
                    elif num < ord('a'):
                        num +=26
    
                decrypted_text += chr(num)
            else:
                decrypted_text += letter
        return decrypted_text
    
    print 'Decryted text is : '
    print decrypt(ciphertext, shift)
    [/I][/I]
    now when i run this program it says shift and ciphertext are not defined. I understand that they aren't defined im just having difficulty in knowing where and how to define these to parameters so that my program works.

    If you coul help or point me in the right direction that would be great.

    Thanks


    oh and heres what im testing it on :

    decrypt('bcdefg hijklmnopqrstuv wxyza', 1)
    'abcdefghijklmn opqrstuvwxyz'

    very simple i know, but im a n00b at python :)
    any help would be greatly appreciated.
    Last edited by bvdet; Apr 7 '10, 06:01 AM. Reason: Add code tags
  • barks33
    New Member
    • Apr 2010
    • 2

    #2
    and it all formatted wrong... i had all the correct spacing by the way just failed in the box.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      The error message is telling you that the variables are not defined. That gives it away. You need to assign a value to the variables.
      Code:
      ciphertext = 'bcdefghijklmnopqrstuvwxyza'
      shift = -1
      You also had a typo with the name decrypted_text and an undefined variable named key.

      Comment

      Working...