"Encrypt" String If A Char Appears In A Dictionary
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))
- I ask the user how long he wants the dict to be and create a dictionary
- I ask for a char as key and num as corresponding value
- I encrypt the string I get by checking if it's in
d1.keys()
- Then I call all the functions to return the encrypted string
I'm not sure where I'm wrong. Any help would be great.
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.
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module