Home > Software engineering >  Looping String in Dictionary
Looping String in Dictionary

Time:02-15

I'm trying to loop a string of keys into the dictionary so as to retrieve the values in the dictionary.

Could anyone help to see why is my code not returning the output expected?

def decipher_message(msg: str, guide:dict):
    message = []  
    for letter in msg:
        result = guide.get("letter")
        message.append(letter)
        
    return message

The rationale behind my code is that i'll first create an empty list of message and for each letter in the msg (string inputted by users), it returns the value of the dictionary. After which I'll append the value and store it in the list []. At the end of it, returning the list of values. Not exactly sure which step went wrong so would appreciate any experts to contribute and help a beginner out here.

I'm not supposed to be hard-coding both strings and dictionary into my function but for example purposes, the following is the string and dictionary that my function is trying to read:

string: "esbtr dgh abzqg! vhe ghz yzqtcjxx qx qt btgesjz cbxepj!"

dictionary: {'a': 'm', 'b': 'a', 'c': 'c', 'd': 'y', 'e': 't', 'f': 'v', 'g': 'o', 'h': 'u', 'i': 'x', 'j': 'e', 'k': 'j', 'l': 'w', 'm': 'f', 'n': 'z', 'o': 'd', 'p': 'l', 'q': 'i', 'r': 'k', 's': 'h', 't': 'n', 'u': 'g', 'v': 'b', 'w': 'q', 'x': 's', 'y': 'p', 'z': 'r'})

The expected output should be: thank you mario! but our princess is in another castle!

CodePudding user response:

You have 2 errors: one guide.get("letter") and one in message.append(letter).

  • For the former error, You are literally searching for the key "letter" in this case. Change it to guide.get(letter). You could also change guide.get(letter) to guide.get(letter, letter). The second argument is optional, and corresponds with the value that should be returned whenever the key is not found. This is useful to extract the spaces and exclamation marks in your string.

  • For the latter, you should append the letter that you extracted from the dictionary,and not the letter itself. Change it to message.append(result) and you should be good to go!

  • At this point, you will still have a list as return object. If you really want to have it as a string, you can join the elements of the list together to a string:

    message_as_string = ''.join(message)
    

If you do not understand why this is the solution, leave a comment and I can elaborate a bit more!

CodePudding user response:

I think this is what you're trying to do:

string = "esbtr dgh abzqg! vhe ghz yzqtcjxx qx qt btgesjz cbxepj!"
dictionary = {'a': 'm', 'b': 'a', 'c': 'c', 'd': 'y', 'e': 't', 'f': 'v', 'g': 'o', 'h': 'u', 'i': 'x', 'j': 'e', 'k': 'j', 'l': 'w', 'm': 'f', 'n': 'z', 'o': 'd', 'p': 'l', 'q': 'i', 'r': 'k', 's': 'h', 't': 'n', 'u': 'g', 'v': 'b', 'w': 'q', 'x': 's', 'y': 'p', 'z': 'r'}

def decipher_message(msg: str, guide: dict) -> str:
    message = []
    for character in msg:
        message.append(guide.get(character, character))
    return ''.join(message)

print(decipher_message(string, dictionary))

Or, if you want it to be really concise then:

def decipher_message(msg: str, guide: dict) -> str:
    return ''.join(guide.get(c, c) for c in msg)
  • Related