0

My code (now):

import cs50
import sys

if len(sys.argv) != 2:
    print("usage: python caesar.py key")
    exit(1)
else:
    key = sys.argv[1]
    c = int(key) % 26
    cphtxt = 0
    if c < 1:
        print("bigger key than 0 please")
        exit(2)

    print("plaintext:")
    plaintext = cs50.get_string()
    print("ciphertext:")
    for i in range(len(plaintext)):
        if(plaintext[i].isalpha):
            if(plaintext[i].isupper):
                cphtxt = ((ord(plaintext[i]) - 65 + c) % 26) + 65
                print(chr(cphtxt), end = "")
            else:
                cphtxt = ((ord(plaintext[i]) - 97 + c) % 26) + 97
                print(chr(cphtxt), end = "")
        else:
            print(plaintext[i])
    exit(0)

The error is gone, but now a couple of issues.

The first one is the output is all capital. If I type in "Hello", out comes "LOVVY" When "Lovvy" should be coming out.

The second one is non alpha characters come out shifted. If I type in ",", out comes "J".

How do I fix this? Thanks,

A (confused) ian

2

From the Hints section of the spec:

You might find chr and/or ord of help.

Python does not treat plaintext[i] the same way that C does. That's where ord comes in.

Python int is more like atoi in C. That's where chr comes in.

2
  • please where is the easiest page to understand chr and ord ?
    – Genevie
    Sep 2 '17 at 18:02
  • Follow the links in the answer to the Python doc on built-in functions. Sep 4 '17 at 12:26
2

I think the problem is that you have a () around the if(plaintext[i].isalpha): when it should be if plaintext[i].isaplha(), and the same for isupper and islower. I had the same problem but when I moved the () just to the end my code worked. Hope this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .