0

I have a functional version of "Vigenère" for pset6 but I noted an issue with the location of a variable that I would appreciate some insight on.

While iterating through each letter of the plaintext I have two important variables:

  1. "counter" - this keeps track of letter in cipherkey (named "key") to be used (it is modulo'd later with the length of the cipherkey in order to loop through it)
  2. "keyCode" - this takes counter, performs modulo on it, and then converts that letter of the cipherkey to Ascii. Finally it removes 97 from it (the value of 'a' in ascii). The cipherkey had been converted to lower case earlier which is why I take away 97.

I have inserted my code below however I have noted that the location of the variable "counter" seems unimportant in my code (as long as I declare it before using it), however the location of "keyCode" is! In the example pasted below the code is fully functional. HOWEVER I have placed a break in my code where it originally was.

When the loop iterates through plaintext, it updates "counter" by 1 each time a letter is encrypted. This is as expected.

However if I put "keyCode" in its original location, it's value never gets updated, despite the fact that it is dependent on the value of "counter" which is updated successfully. I.e. The value it obtains when originally declared (which uses a counter value of 0) is the the only value it takes.

import cs50
import sys

# Check for single, alphabetical command line argument, else exit with code 1
if len(sys.argv) != 2 or sys.argv[1].isalpha() == False:
    print("You must enter a single, alphabetical cipherkey. Usage: python caesar.py key")
    exit(1)

#Variable called key that takes the command line argument, converts to lowercase
key = sys.argv[1].lower()

#Get the plaintext to be converted
print("plaintext: ", end="")
plain = cs50.get_string()

print("ciphertext: ", end="")

#Counter to keep note of letter in cipherkey to use
counter = 0

This is the original location where "keycode" was declared

#Iterate through each character of plaintext:
for character in plain:
#check if character is alphabetical
    if character.isalpha():
        #Variable that works out key value to be used
        keyCode = ord(key[counter % len(key)]) - 97
        if character.isupper():
            #Convert the character to the cipher by adding key and then modulo it
            cipher = 65 + ((ord(character) - 65 + keyCode) % 26)
            print(chr(cipher), end="")
            counter += 1
        else:
            #Convert the character to the cipher by adding key and then modulo it
            cipher = 97 + ((ord(character) - 97 + keyCode) % 26)
            print(chr(cipher), end="")
            counter += 1
    else:
        #If not a letter of the alphabet then just print the character
        print(character, end="")
print()

exit(0)

Thank you for your help. I would also be grateful if anyone can suggest improvements/issues with my code in general.

0

You need to adjust your thinking here "...it is dependent on the value of "counter" which is updated successfully.". It is not dependent on the value of counter through the life of the variable, that would require a function of some kind. In the original location, it is set based on the value of counter at that time, and will not change until it is set to something else (as in the working version).

| improve this answer | |
  • Ahh ok. I can see the reason now. Thank you for the explanation. – Souper Aug 2 '17 at 8:45

You must log in to answer this question.

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