0
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)

I have a couple of errors.

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

1

isalpha and isupper are built-in methods that return a boolean. Therefore, the correct notation is isalpha() and isupper().

To get "eyes on", you can call interactive python at the command line and experiment a little. For example:

>>> a = "A"
>>> a.isupper
<built-in method isupper of str object at 0x7f617f37c298>
>>> a.isupper()
True
>>> 
| improve this answer | |

You must log in to answer this question.

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