Why does this code fail? Decode a cipher


#1

Hello everyone,

I’m new to Python and programming in general. Therefore, I’m doing the Python 3 course here at codecademy.
I’m now at the “coded communication” task (https://www.codecademy.com/courses/learn-python-3/informationals/python3-coded-communication):
Here, a ciper should be decoded by a “vigenere cipher” with the keyword phrase “friends”.

I really don’t understand why the following could does not work correctly and these mysterious “u”'s appear…
I would be really glad, if anyone can help me!

Here is the problematic code:

hmessage = "dfc jhjj ifyh yf hrfgiv xulk? vmph bfzo! qtl eeh gvkszlfl yyvww kpi hpuvzx dl tzcgrywrxll!"

abc = "abcdefghijklmnopqrstuvwxyz"

key = "friends"

dechiff = ""
keystep = 0
def decode(code, abc, key):
    global dechiff
    global keystep
    for i in code:
            if keystep < len(key):
                if i in abc:
                    dechiff += abc[abc.find(i)-abc.find(key[keystep])]
                    keystep += 1
                            
                else:
                    dechiff += i
                    keystep += 1
            else:
                keystep = 0
                dechiff += abc[abc.find(i)-abc.find(key[keystep])]
                keystep += 1

            
decode(hmessage, abc, key)

print(dechiff)

Best regards,
André


#2

You can use prints to write out what is going on during translation. A good start would be to write out the input char and the output char, and then you’d want to look closer at what choices are being made when the output is those u’s


#3

In terms of making things easier for yourself, a large simplification would be to, before you do anything else in the loop: figure out the current letter and the key.
then (instead of nesting things inside each other) translate the letter


#4

Thanks man!

I forgot that the find() function return -1 if it doesn’t find anything…

My new code solved the problem!

hmessage = "dfc jhjj ifyh yf hrfgiv xulk? vmph bfzo! qtl eeh gvkszlfl yyvww kpi hpuvzx dl tzcgrywrxll!"

abc = "abcdefghijklmnopqrstuvwxyz"

key = "friends"

dechiff = ""
keystep = 0
sy = {"!", "?", " "}
def decode(code, abc, key):
    global dechiff
    global keystep
    for i in code:
            if keystep < len(key):
                if i in abc:
                    dechiff += abc[abc.find(i)-abc.find(key[keystep])]
                    print(i + "->" + abc[abc.find(i)-abc.find(key[keystep])])
                    keystep += 1        
                else:
                    dechiff += i
                    keystep += 1
                    print(i + "->" + i)
            else:
                if i not in sy:
                    keystep = 0
                    dechiff += abc[abc.find(i)-abc.find(key[keystep])]
                    print(i + "->" + abc[abc.find(i)-abc.find(key[keystep])])
                    keystep += 1
                    
                else:
                    keystep=0
                    dechiff += i
                    keystep += 1
                    print(i + "->" + i)

            
decode(hmessage, abc, key)

´´´

#5

try doing this instead:

for each position
    here's the message letter
    should it be translated? if not, skip this iteration (continue)
    here's the key letter
    here's the translated letter

(note that it doesn’t wander off to the right)
there’s a condition there, but none of the rest would be nested inside it.
you’ll avoid a whole lot of duplication too


#6

Thanks for the hint!

In the end, I had a complety different solution (below)
But since I could not understand why the other code didn’t work, I posted it here.

hmessage = "dfc jhjj ifyh yf hrfgiv xulk? vmph bfzo! qtl eeh gvkszlfl yyvww kpi hpuvzx dl tzcgrywrxll!"

abc = "abcdefghijklmnopqrstuvwxyz"

key = "friends"

dechiff = ""
while len(key) < len(hmessage):
    key += key
    
keystep = 0  

def decode(code, abc, key):
    global dechiff
    global keystep
    for i in code:
        if i in abc:
            dechiff += abc[abc.find(i)-abc.find(key[keystep])]
            keystep += 1
                        
        else:
            dechiff += i
            keystep += 1
            

            
decode(hmessage, abc, key)

print(dechiff)
´´´

#7

extending the key helps as well, yeah.