Home > front end >  Cesar Coding question using .get method for dictionaries
Cesar Coding question using .get method for dictionaries

Time:11-03

I'm doing a homework where it's asking me to fill out the blanks in the below function using .get method for dicts. The problem I am having is that when I try to use cipher.get() method, I don't know what to pass through the method for the key:value pairs. Bascially I want the .get method to return the encrypted letter if it is found in the dict, and return the original character if it is not found in the dict.

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
cipher = {letters[i]: letters[(i-3) % len(letters)] for i in range(len(letters))}



def transform_message(message, cipher):
    tmsg = ''
    for c in message:
        tmsg = tmsg   ___
    return tmsg

CodePudding user response:

Inside the .get() function, you need to pass c as an argument in order to get the ciphered letter from c like that:

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
cipher = {letters[i]: letters[(i-3) % len(letters)] for i in range(len(letters))}



def transform_message(message, cipher):
    tmsg = ''
    for c in message:
        tmsg  = str(cipher.get(c,c))
    return tmsg
  • Related