Ad

"Encrypt" String If A Char Appears In A Dictionary

- 1 answer

I'm stuck with an issue with dictionaries.

So my dictionary has a char in the alphabet as key, and a number as value.

I need to enter a string, and then if a char in that string appears in my dictionary, I need to replace that char with that corresponding key's value.

So for example, this is my dictionary: a:1, b:2, c:3, d:4. For a string like this: dan I will get the output: 41n.

If the letter is not in the dictionary, then it just stays as is.

This is my try:

    def build_encrypt_code():
        d1 = {}
        dictLength = int(input("How many keys do you want in your dict? "))
        for i in range(dictLength):
            keyLetter = str(input("Please enter a char to represent a key: "))
            valueNum = int(input("please enter a number to represent the key's value: "))
            d1[keyLetter] = valueNum
        return d1

    def encrypt(d1, strToEncrypt):
        for i in strToEncrypt:
            if i == d1.keys():
                encrpytedStr = strToEncrypt.replace(i, d1[i])
                return encrpytedStr
            else:
                return i

    d1 = build_encrypt_code()
    strToEncrypt = str(input("Enter string to encrypt: "))
    print(encrypt(d1, strToEncrypt))
  1. I ask the user how long he wants the dict to be and create a dictionary
  2. I ask for a char as key and num as corresponding value
  3. I encrypt the string I get by checking if it's in d1.keys()
  4. Then I call all the functions to return the encrypted string

I'm not sure where I'm wrong. Any help would be great.

Ad

Answer

I made two edits to your code.

1. With the first edit you can parse keys and values without asking user how many he wants, using a while True and stopping when finding a exit command (like the string stop).

There is also a check on length (keyLetter is a single char, valueNum is a single digit, thanks to the two while len() != 1, prompting the user to insert a value again if they fail before)

This is subjective; if you need a fixed size before, you're good to go with your code.

def build_encrypt_code():
    d1 = {}
    exitCmd = 'stop'
    print('Enter {} to interrupt parsing'.format(exitCmd))

    while True:
        keyLetter = ''
        while len(keyLetter) != 1:
            keyLetter = str(input("Please enter a char to represent a key: "))
            if keyLetter.lower() == exitCmd:
                break

        valueNum = ''
        while len(str(valueNum)) != 1:
            valueNum = int(input("please enter a digit to represent the key's value: "))

        d1[keyLetter] = valueNum

    return d1

2. The second edit, however, is mandatory, and is where your software fails.

def encrypt(d1, strToEncrypt):
    for i in strToEncrypt:
        if i == d1.keys():
            encrpytedStr = strToEncrypt.replace(i, d1[i])
            return encrpytedStr
        else:
            return i

In this function, you cycle over all characters in your string (i is a character), and then you compare a character with a KeysView. Furthermore, you then return inside the if, but doing so it will do only a cycle and then break (and, because the if condition is always False, your encryptedStr will always be your first character).

This is the working code for encrypt function:

def encrypt(d1, strToEncrypt):
    encryptedStr = strToEncrypt

    for char in strToEncrypt:
        if char in d1.keys():
            encryptedStr = encryptedStr.replace(char, str(d1[char]))

    return encryptedStr

We cycle over every character, and if it's in the KeysView, you can replace this char with the string version of your number (associated to that char), and then at the end of the for you can return the string encrypted.

Ad
source: stackoverflow.com
Ad