0

I'm sorry to make yet another thread about this program, but I thought that since the other two were about specific error messages, I'd make another one for help on figuring out what I'm doing wrong and finishing the program. So here I am.

The code:

import cs50
import sys

if len(sys.argv) == 1 or len(sys.argv) > 2:
    print("Usage: python vigenere.py <key>")
    exit(1)

key = sys.argv[1]
for i in range(len(key)):
    if key[i].isalpha() is not True:
        print("Keyword must only contain letters A-Z or a-z")
        exit(2)

print("plaintext: ", end="")
plaintext = cs50.get_string()
plaintext_size = len(plaintext)
key_size = len(key)

ciphertext = []
j = 0
for i in range(plaintext_size):
    if j >= key_size:
        j = 0
    if plaintext[i].isalpha():
        k = ord(key[j].upper()) % 65

        c = plaintext[i]
        c = ((c + k - 65) % 26) % 65
        ciphertext.append(chr(c))
        j += 1

    else:
        c = ord(plaintext[i])
        ciphertext.append(chr(c))

print("ciphertext: ", end="")
print("".join(ciphertext))
exit(0)
0

The first % 65 probably meant - 65, though both work the same here, would be different only if there were valid results of upper that were not in range 65..129.

The second % 65 would better be + 65, and you'd use ord also on c. For plaintext[i].islower(), you'd use - 97 and + 97 on that line instead.

| improve this answer | |
  • Thanks for the reply. I tried to this: c = ord(plaintext[i]) and c = ((c + k - 65) % 26) % 65, but it's still giving me an erroneous result. With the plaintext "Meet me at the park at eleven am" and while using the keyword "bacon", all I got is a large amount of blank space as output. – Osman Zakir Feb 27 '17 at 13:46
  • Told you to use +65, and also to differentiate between upper- and lower-case. Should be more like c = ((c + k - 65) % 26) + 65 if c<=90 else ((c + k - 97) % 26) + 97 – Blauelf Feb 27 '17 at 14:04
  • So put the first one under an if texting for whether or not c is uppercase and the second one should be if it's lowercase? – Osman Zakir Feb 27 '17 at 15:12
  • I wrote it as an if-else expression, as that does not require indentation, but you can do a full if c<=90: and else: one with 65, and the other with 97. – Blauelf Feb 27 '17 at 15:22
  • Thanks. It worked. – Osman Zakir Feb 27 '17 at 15:30

You must log in to answer this question.

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