1

I'm attempting a Vigenere cipher for a class. I'm to use the key word "friends" to unlock the coded message. I can't seem to get the values of the key phrase to add to the values of the coded phrase correctly. I think somehow I'm not iterating through the 'key' properly, but I'm unsure how to trouble shoot.

#following code will attempt to use the key word 'friends', to unlock the coded phrase stored under 'message'.
    message ="dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!"
    alpha = "abcdefghijklmnopqrstuvwxyz"
    punc = ".!? \/$@^%&*'"
    
    #defining function that will take the coded 'message' and the 'key' phrase, as inputs.
    def decoder(message, key):
        decoded = ""
    #attempting to iterate through the key phrase to obtain it's value, print() here is used to test that the values are seen
        for k in key:
            if k not in punc:
                key_val = alpha.find(k)
                print(key_val)
    #iterating through the 'message' variable to find it's values and exclude the punctuation/spaces.Then, add the 'message'
    #values and 'key' values to get the coded message. 
        for letters in message:
            if letters not in punc:
                message_val = alpha.find(letters)
                decoded += alpha[(message_val + key_val) % 26]
            else:
                decoded += letters
        return decoded
    #output is incorrect.
    decoder(message,"friends")
mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0
for letters in message:
    if letters not in punc:
        message_val = alpha.find(letters)
        key_val=alpha.find(key[counter])
        print(key[counter])
        decoded += alpha[(message_val + key_val) % 26]
        counter+=1
        if counter==7:
            counter=0

    else:
        decoded += letters
return decoded
gogaloh
  • 1
  • 1
  • So when I run this I get "TypeError: string indices must be integers" – Peter DeMore Sep 10 '22 at 16:32
  • It highlights key[counter] – Peter DeMore Sep 10 '22 at 16:33
  • Add def decoder(message, key): decoded = "" counter=0 key_val=0 – gogaloh Sep 11 '22 at 02:23
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 15 '22 at 23:01