Okay, this is heavily frustrating. First, time ever using Python. Code compiles but when input the following: python caesar.py 13 plaintext: hello, world I get, ciphertext: hello, world. I have no clue how it's not working when I put in caesar's precisely. Also, if I put ALL CAPS in plaintext, I get an " local variable key reference before assignment." Referred to other peoples code and cannot solve this issue.

from cs50 import get_string
import sys

def main():
    if len(sys.argv) != 2:
        print("Usage: error")#check if enough arguements are passed

        key = int(sys.argv[1])#gets key as int and validates

    pretext = get_string("plaintext: ")
    print("cipertext: ", end="");

    for i in range(len(pretext)):#enciphers length of string

        if str.isupper(pretext[i]):#uppercase letters
            uppercase = ((ord(pretext[i]) - 65 + key)% 26)+65
            print(chr(uppercase), end="")
        elif str.isupper(pretext[i]):#lowercase letters
            lowercase = ((ord(pretext[i])- 97 + key)% 26)+ 97
            print(chr(lowercase), end="")
            print("{}".format(pretext[i]), end="")


if __name__=="__main__":

If you have no capital letters at all, everything is printed by the last else path. That's because you have str.isupper twice, instead of islower on the elif.

key is not defined because it is assigned to inside the if len(sys.argv) != 2: block, at a place not reachable. Remove one level of indentation on the key = int(sys.argv[1]) so that it's the same indentation level as the pretext = .... And maybe add some handling for when the key is not numeric.

  • I looked at that so much. I think my issue was just that I up all night so my vision was just terrible haha. Thank you.
    – user24000
    Aug 20 '19 at 23:15

You must log in to answer this question.