For Pset6 Python, Vigenere.py does not encrypt any message with any key. It only prints out the given plaintext, not encrypted. Any help would be greatly appreciated. Thanks in advance.


#Encrypts a message using vigenere's cipher using a user-given key
from cs50 import get_string
from sys import argv

    def main():
    #Ensures proper usage
    if not len(argv) == 2:
        exit("Usage: ./caesar key(purely alphabetical key)")
    #Ensures string is alphabetical
    key = argv[1]
    isalpha = True
    for c in key:
        if key.isalpha == False:
            exit("Usage: ./caesar key(purely alphabetical key)")
    #Gets plaintext from user
    plaintext = get_string("plaintext: ")
    ciphertext = []
    k = 0
    #Encrypts message
    for c in plaintext:
        #Encrypts single letter
        ciphertext.append(encrypt(c, key[k]))
        #Updates which letter of the key is in use
        if c.isalpha:
            k = (k + 1) % len(key)
    #Prints ciphertext
    print("ciphertext: ", end = "")
    for c in ciphertext:
        print(c, end = "")

def encrypt(x, y):
    #Proceeds to encrypt only if char is alphabetical
    if x.isalpha == True:
        isuppur = False
        isupper = False
        #Checks case of plaintext letter
        if x.upper == True:
            isuppur = True
        #Checks case of key letter
        if y.upper == True:
            isupper = True
        numalpha = ord(x)
        alphanum = ord(y)
        #Converts both letters to ints
        if isuppur == True:
            numalpha -= 65
            numalpha -= 97
        if isupper:
            alphanum -= 65
            alphanum -= 97
        #Applies formula to numalpha to change value
        numalpha = (numalpha + alphanum) % 26
        #Changes back to ASCII
        if isuppur == True:
            numalpha += 65
            numalpha += 97
        #Changes back to char format
        z = chr(numalpha)
        return z
    #Returns given char if isalpha returns false
        return x

if __name__ == "__main__":

1 Answer 1


A problem lies in the use of isalpha. It is a string method, therefore the syntax is str.isalpha(), which returns a boolean. When str.isalpha is exectuted, python returns something like this <built-in method isalpha of str object at 0x7f8bb6270688>. You will probably want to review the use of the string method isupper as well.

You must log in to answer this question.

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