Dictionary ciphertext problem hint.

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

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

password = "h knud xnt"


def decode(dictionary, password):
    word = tuple(str(password))
    for word in dictionary.keys():
        ans = dictionary[word]
    return ans

print(decode(dictionary,password))

I'm trying to define a def function that exports a dictionary's value an by putting a password key value, but it's too difficult to see in seconds. I need to modify the word part of the code above, so please give me a hint.

dictionary python

2022-09-20 13:13

1 Answers

It's simple if you read the purpose and code in order.

word = tuple(str(password)) tuples password. Since password is "hknud xnt", word will be ('h', ', 'k', 'n', 'u', 'd', ', 'x', 'n', 't').

for word in dictionary.keys(): patrols the key value of dictionary and the key is substituted for word. That is, loop around "a" and loop around "b"... loops for all keys for dictionary.

However, the value of word that we tupled earlier at this time will disappear due to the for statement, right?

What you want to do is find each letter of word with a key in dictionary and combine it into the original sentence. Therefore, the tuple value must not disappear.

And in the for statement, you should not go around with the key of dictionary but you should go around with the letter of word.

Therefore, if you have a tupled word and write a for statement to go around the loop, you can find it in dictionary for each letter.

for w in word:
    ans = dictionary[w]

But then you just find each letter, not the original sentence.

You can combine the characters found in dictionary into strings as follows:

ans = ""
for w in word:
    ans += dictionary[w]

As a result, "hknud xnt" is converted to "i love you".


2022-09-20 13:13

If you have any answers or tips


© 2022 pinfo. All rights reserved.