The Vigenère Cipher - % help

Hi!
I am working on Codecademy’s Python3 course; the Off-Platform Project for strings called Coded_Correspondence. I’m having trouble with my Python code to decode The Vigenère Cipher. I think the issue is somewhere near where I need to wrap around the alphabet using the % operator.

Any help or insights would be so appreciated!!

This is the course page:
https://www.codecademy.com/courses/learn-python-3/informationals/python3-coded-communication

A good explanation of The Vigenère Cipher:
https://www.dcode.fr/vigenere-cipher#:~:text=To%20decrypt%2C%20take%20the%20first,rank%20of%20the%20plain%20letter.

Here is my code:

# Vigenere Cipher: plaintext value + key value = coded value (index off of alphabet 0 - 25)

V_Cipher_Message = "dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!"
keyword = "friends"


def V_Cipher_Decoder(message, keyword):
    uncoded_message = ""
    # keep track of keyword character index, will need to loop
    i = 0
    
    for character in message:
        # for lower case letters
        if (97 <= ord(character) <= 122):
            # code_value_index = character value + keyword value (keyword character changes each time)
            # write this remember character is coded value
            # also to get index value between 0 - 25
            codedtext_value_index = ord(character) - ord("a")
            # index not looped around alphabet
            if (0 <= (codedtext_value_index - (ord(keyword[i]) - ord("a"))) <= 25):
                uncoded_message += chr((codedtext_value_index - (ord(keyword[i]) - ord("a"))) + ord("a"))
                # iterate through keyword, loop
                if (0 <= i <= (len(keyword) - 2)):
                    i += 1
                else:
                    i = 0
        
            # index looped around alphabet
            else:
                uncoded_character_index = (codedtext_value_index - (ord(keyword[i]) - ord("a"))) % 26
                uncoded_message += chr(uncoded_character_index + ord("a"))

                # iterate through keyword, loop
                if (0 <= i <= (len(keyword) - 2)):
                    i += 1
                else:
                    i = 0
            
        # for spaces and symbols
        else:
            # for character straight copy
            uncoded_message += character
            
            # iterate through keyword, loop
            if (0 <= i <= (len(keyword) - 2)):
                i += 1
            else:
                i = 0
    
    print(uncoded_message)
    
V_Cipher_Decoder(V_Cipher_Message, keyword)    

That shouldn’t be the problem, Python does allow that kind of chaining with comparison operators https://docs.python.org/3/reference/expressions.html#comparisons

import string # check all lowercase... print(all(97 <= ord(char) <= 122 for char in string.ascii_lowercase)) char = chr(96) # the ` char print(97 <= ord(char) <= 122 ) char = chr(123) # the { char print(97 <= ord(char) <= 122 )

@sharheinitz
I’d suggest turning this into one or more functions at present (if you can’t spot another way to simplify it), there’s a few cases of repeats and some deep nesting which is probably better to avoid (several layers of indentation is very hard for humans to follow). You might also be able to avoid certain if checks just using % alone (if you want to)…

29 % 26 == 3
3 % 26 == 3
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.