0

I seem to be having some problems with Vigenere.py for problem set 6. I am attempting to use ord to change the ith character in the plaintext into ASCii, but it is not working. Everything else seems to be logical I think?

Thank you for your help.

import sys

if len(sys.argv) == 2:
    key = str(sys.argv[1])

    if str.isalpha(key) == True:
        plaintext = str(input("Enter the plaintext you wish you encrypt: "))
        print ("ciphertext = ", end='')

        for i in plaintext:
            plaintext = ord(str(plaintext[i]))

            #preventing long plaintext words from not being encrypted
            if i > len(ciphertext) or k > len(ciphertext):
                k = 0
                cipher = ord(str(ciphertext[k]))
                k += 1
            else:
                ciphertext = ord(str(ciphertext[i]))
            #

            final = (plaintext + ciphertext)%26

            print (chr(final), end='')
    else:
        print ("Enter only letters")

else:
    print ("Usage: Enter a key to use to ciphers")
0

1 Answer 1

1

Go back to your C solution and try to translate it into Python. Not word by word, but by meaning. This code is not a transcript of a working C solution, it's far from a working solution at all.

    key = str(sys.argv[1])

No need to convert this to a string, as it already is.

    if str.isalpha(key) == True:

Just if str.isalpha(key): would be enough.

        plaintext = str(input("Enter the plaintext you wish you encrypt: "))
        print ("ciphertext = ", end='')

Are those the strings required per specification?

        for i in plaintext:

i will assume the characters of plaintext, it's not a number. You can use i instead of plaintext[i], or use for i in range(len(plaintext)):

            plaintext = ord(str(plaintext[i]))

No need for str. You replace string plaintext with a number. Better find another variable.

            if i > len(ciphertext) or k > len(ciphertext):

You never created variables k and ciphertext, so what should k > len(ciphertext) mean?

                k = 0
                cipher = ord(str(ciphertext[k]))
                k += 1

So after this code block, which is the only place where k is assigned to, it always has value 1. Also, str not needed.

                ciphertext = ord(str(ciphertext[i]))

Multiple things from above apply here, too.

            final = (plaintext + ciphertext)%26
            print (chr(final), end='')

In the C solution, you shifted the characters back and forth a bit, ignoring case for the key and preserving case for the character to encrypt.

You must log in to answer this question.

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