Off-Platform Project: Coded Correspondence

Hey,

I was working on this project. My thinking and with the help of Google, got me to this solution. It appears to work till the iteration hits the first punctuation and everything get messed up.

The iteration through the keyword will continue with punctuation and the error will keep accumulating through the rest of the message.

Is there a way to fix it in its current format so that it works?

Thanks a lot in advance.

Abdalla

alphabet = "abcdefghijklmnopqrstuvwxyz"
punctuation = ".,?'! "
message = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!"
translated_message = ""
keyword = "friends"

from itertools import cycle

for m, k in zip(message, cycle(keyword)):

    if not m in punctuation:
        letter_value = alphabet.find(m) - alphabet.find(k)
        translated_message += alphabet[(letter_value) % 26]

    else:
        translated_message += m

print(translated_message)

We do not see where keyword is defined. What should the argument for cycle be?

Sorry, I have added the keyword.

Is there a landing page for this project?

Here you go:
https://www.codecademy.com/courses/learn-python-3/informationals/python3-coded-communication

Please find below the relevant part from the exercise. The whole project can be found in Jupyter notebook on the exercise page: [https://www.codecademy.com/courses/learn-python-3/informationals/python3-coded-communication ]

Step 5: The Vigenère Cipher

Great work! While you were working on the brute force cracking of the cipher, Vishal sent over another letter. That guy is a letter machine!

Salutations! As you can see, technology has made brute forcing simple
ciphers like the Caesar Cipher extremely easy, and us crypto-enthusiasts have had
to get more creative and use more complicated ciphers. This next cipher I’m going
to teach you is the Vigenère Cipher, invented by an Italian cryptologist named
Giovan Battista Bellaso (cool name eh?) in the 16th century, but named after
another cryptologist from the 16th century, Blaise de Vigenère.

The Vigenère Cipher is a polyalphabetic substitution cipher, as opposed to
the Caesar Cipher which was a monoalphabetic substitution cipher. What this
means is that opposed to having a single shift that is applied to every letter, the
Vigenère Cipher has a different shift for each individual letter. The value of the
shift for each letter is determined by a given keyword.

Consider the message

       barry is the spy

If we want to code this message, first we choose a keyword. For this
example, we’ll use the keyword

       dog

Now we use the repeat the keyword over and over to generate a keyword
phrase
that is the same length as the message we want to code. So if we want to
code the message “barry is the spy” our keyword phrase is “dogdo gd ogd ogd”
. Now we are ready to start coding our message. We shift the each letter of our
message by the place value of the corresponding letter in the keyword phrase,
assuming that “a” has a place value of 0, “b” has a place value of 1, and so forth.
Remember, we zero-index because this is Python we’re talking about!

                message:       b  a  r  r  y    i  s   t  h  e   s  p  y

         keyword phrase:       d  o  g  d  o    g  d   o  g  d   o  g  d

  resulting place value:       4  14 15 12 16   24 11  21 25 22  22 17 5

So we shift “b”, which has an index of 1, by the index of “d”, which is 3. This
gives us an place value of 4, which is “e”. Then continue the trend: we shift “a” by
the place value of “o”, 14, and get “o” again, we shift “r” by the place value of “g”,
15, and get “x”, shift the next “r” by 12 places and “u”, and so forth. Once we
complete all the shifts we end up with our coded message:

        eoxum ov hnh gvb

As you can imagine, this is a lot harder to crack without knowing the
keyword! So now comes the hard part. I’ll give you a message and the keyword,
and you’ll see if you can figure out how to crack it! Ready? Okay here’s my
message:

dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!

and the keyword to decode my message is

friends

Because that’s what we are! Good luck friend!

And there it is. Vishal has given you quite the assignment this time! Try to
decode his message. It may be helpful to create a function that takes two
parameters, the coded message and the keyword and then work towards a
solution from there.

NOTE: Watch out for spaces and punctuation! When there’s a space or
punctuation mark in the original message, there should be a space/punctuation
mark in the corresponding repeated-keyword string as well!

I copied it from step #1 in the same exercise. I was just trying things around.

Just wondering, is it possible to use this approach of iterating through both lists and taking into account the punctuation?

Thanks :slight_smile:

You’re going to step through both of them. So yeah you’re iterating through them.

But they should not be iterated through in lock-step with one-another.

I ran into the same issue when I was working this problem. Try to start thinking about how you could check whether or not the item int he string is a character that needs shifting, or if it is something else that you would just like to carry over to your answer string.