Off-Platform Project Coded Correspondence #5: syntax error

Link for this exercise:
https://www.codecademy.com/paths/data-science-foundations/tracks/dsf-python-fundamentals-for-data-science-part-ii/modules/dsf-python-strings/projects/coded-correspondence-project

I’m working on decoding the Vigenere cipher which requires a different shift for every letter in the encrypted message. It currently states that I have a syntax error which I didn’t think was wrong.

Does anyone have suggestions on what I need to change to get the correct output? I’ve added in three code blocks below, my current code (A), the error message it spins out (B) and the official Codecademy solution (C).

- Code block A: My code

def vig_decode(message, keyword):
  alphabet = 'abcdefghijklmnopqrstuvwxyz'
  punctuation = ',.!? '
  keyword_position = 0
  translated_message = ''
  for i in range(0,len(message)):
    if message[i] in punctuation:
      translated_message += message[i]
      keyword_position = (keyword_position + 1)%len(keyword)
    else:
      translated_letter_index = (alphabet.find(message[i%25]) + alphabet.find(keyword[((i + keyword_position)%25)])
      translated_letter = alphabet[translated_letter_index]
      translated_message += translated_letter
      return translated_message
  print(translated_message)

vig_decode('txm srom vkda gl lzlgzr qpdb? fepb ejac! ubr imn tapludwy mhfbz cza ruxzal wg zztcgcexxch!', 'friends')

- Code Block B: The Error

  File "script.py", line 12
    translated_letter = alphabet[translated_letter_index]
    ^
SyntaxError: invalid syntax

- Code Block C: The Official Solution

def vigenere_decoder(coded_message, keyword):
    letter_pointer = 0
    keyword_final = ''
    for i in range(0,len(coded_message)):
        if coded_message[i] in punctuation:
            keyword_final += coded_message[i]
        else:
            keyword_final += keyword[letter_pointer]
            letter_pointer = (letter_pointer+1)%len(keyword)
    translated_message = ''
    for i in range(0,len(coded_message)):    
        if not coded_message[i] in punctuation:
            ln = alphabet.find(coded_message[i]) - alphabet.find(keyword_final[i])
            translated_message += alphabet[ln % 26]
        else:
            translated_message += coded_message[i]
    return translated_message

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

print(vigenere_decoder(message, keyword))

You have a syntax error because on the line
translated_letter_index = (alphabet.find(message[i%25]) + alphabet.find(keyword[((i + keyword_position)%25)])
the first ( does not have a matching )
Remove the ( in front of alphabet.find to fix this.

On that same line, you already have the index of the letter in the keyword as keyword_position so
keyword[((i + keyword_position)%25)] should be just keyword[keyword_position]

You have
keyword_position = (keyword_position + 1)%len(keyword) in the if-block.
It should be in the else-block instead,
because you don’t want to iterate through the keyword if you’re dealing with a punctuation character.

And you could do
translated_letter = alphabet[translated_letter_index % 26]
so that you don’t use an invalid index.

return translated_message
should be outside the loop,
possibly after
print(translated_message)

% 25 should be % 26, since the length of alphabet is 26 (there are 26 letters in the alphabet here)

def vig_decode(message, keyword): alphabet = 'abcdefghijklmnopqrstuvwxyz' punctuation = ',.!? ' keyword_position = 0 translated_message = '' n = len(alphabet) for i in range(0,len(message)): if message[i] in punctuation: translated_message += message[i] else: translated_letter_index = alphabet.find(message[i]) + alphabet.find(keyword[keyword_position]) translated_letter = alphabet[translated_letter_index % n] translated_message += translated_letter keyword_position = (keyword_position + 1)%len(keyword) print(translated_message) return translated_message vig_decode('txm srom vkda gl lzlgzr qpdb? fepb ejac! ubr imn tapludwy mhfbz cza ruxzal wg zztcgcexxch!', 'friends')
1 Like