Home > Software engineering >  Word Decoder Python
Word Decoder Python

Time:10-01

The goal of the below code is to decode one of the below encoded words. The issue arises with the output where it prints the decoded letter and encoded letter at the same time (ie. WBLARF8TTS should = WATERFALLS, but instead = WABTLEARFA8LTLTS)

\\\
def main():
    
    encodedWord = "WBLARF8TTS"
    #encodedWord = "L8KAOUL"
    #encodedWord = "E8N8N8"
    #encodedWord = "8TRA8DY T8LA"
    #encodedWord = "8TT LHA TILLTA LIMAS"   
    #encodedWord = "LHA GRAAN FIATD GTA8MS IN LHA W8RM SUNEABMS"
    #encodedWord = "TONG T8E T8CKS L8SLY L8CO LIMA 8L TA8SL T8LATY"
    
    
    
    #encodedWord = "UUHO"       #Used for Bonus
    #encodedWord = "EOUUUUOUU"  #Used for Bonus
    
    print(DecodeWord(encodedWord))

def DecodeWord(encodedWord):
    word = []
    charList = [*encodedWord]
    for i in charList:
        if i == "L":
            letter = "T"
            word.append(letter)
        if i == "T":
            letter = "L"
            word.append(letter)
        if i == "8":
            letter = "A"
            word.append(letter)
        if i == "B":
            letter = "A"
            word.append(letter)
        if i == "A":
            letter = "E"
            word.append(letter)
        if i == "E":
            letter = "B"
            word.append(letter)
        else:
            letter = i
            word.append(letter)
    print("".join(word))
    
if __name__ == "__main__":
    main()
\\\

CodePudding user response:

Perhaps elif would solve your issues here.

Try:

for i in charList:
        if i == "L":
            letter = "T"
            word.append(letter)
        elif i == "T":
            letter = "L"
            word.append(letter)
        elif i == "8":
            letter = "A"
            word.append(letter)
        elif i == "B":
            letter = "A"
            word.append(letter)
        elif i == "A":
            letter = "E"
            word.append(letter)
        elif i == "E":
            letter = "B"
            word.append(letter)
        else:
            letter = i
            word.append(letter)
  • Related