0

This is what ive done so far but this keeps popping up:

Traceback (most recent call last):
  File "C:/Users/tom/Documents/python 2.py", line 20, in <module>
    print (cypherText)
NameError: name 'cypherText' is not defined

My code:

message = input("what is your message? ").lower()
Shift = int(input("What is your shift? "))

def caesar(message, shift):
    cypherText= " "
    for ch in message:
        if ch.isAlpha():
            stayInAlpha = ord(ch) + shift
        if stayInAlphabet > ord ('z'):
            stayInAlphabet -= 27
        if stayInAlphabet < ord ('a'):
            stayInAlphabet += 27
        finalletter = chr(stayInAlphabet)
        cypherText += finalletter



print ("Your message is:")
print (cypherText)

It is at this last bit where I think it all goes wrong but i have no idea why

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    A quick comment: Any time you are doing anything with crypto, whether it's a simple Caesar cipher, RSA, or Rijndael, you will need to know how to use the modulo operator `%`. See http://stackoverflow.com/questions/4432208/how-does-work-in-python – outis nihil May 15 '14 at 18:55

1 Answers1

3

It appears that cypherText is defined only inside your function.

Just modify your function to return it.

Also, since caesar is defined as function, it needs to be invoked, or else the code inside it will not run. Note that I have not tested your caesar function. If it is broken, it will still be broken.

def caesar(message, shift):
    cypherText= " "
    for ch in message:
        if ch.isAlpha():
            stayInAlpha = ord(ch) + shift
        if stayInAlphabet > ord ('z'):
            stayInAlphabet -= 27
        if stayInAlphabet < ord ('a'):
            stayInAlphabet += 27
        finalletter = chr(stayInAlphabet)
        cypherText += finalletter
    return cypherText

print("Your message is")
print (caesar(message, Shift))
merlin2011
  • 71,677
  • 44
  • 195
  • 329