I am writing a simple string encryption program.

For example: create a list or dictionary (q, w, e, r, t, y1,2,3,4) characters, which will be encrypted.

Next, as I understand it, you need to implement the matching of the entered word with the characters from the list / dictionary, and then perform the replacement of characters and display the result on the screen.

But how to do it?

Closed due to the fact that off-topic participants Enikeyschik , LFC , freim , 0xdb , andreymal January 27 at 16:55 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Enikeyschik, LFC, freim, 0xdb, andreymal
If the question can be reformulated according to the rules set out in the certificate , edit it .

1 answer 1

If you mean creating a dictionary with values, you can do it like this:

dict_with_coding = { 'a' : 1, 'b' : 2, 'c' : 3 } 

Etc. by the list. You can also generate a dictionary using the random module.

You can encode as follows (the method returns a new string, since the strings in Python immutable):

 def code_string(input_str : str, coding_dict) -> str: #Получаем список символов list_with_symbols = list(input_str) for i in range(len(list_with_symbols)): #Этот фрагмент также можно реализовать с помощью метода dict.setdefault #чтобы исключить случаи отсутствия ключа в словаре list_with_symbols[i] = str(coding_dict[list_with_symbols[i]]) return ''.join(sym for sym in list_with_symbols) 

Let's do some tests:

 dict_with_coding = { 'a' : 1, 'b' : 2, 'c' : 3 } print(code_string('aabbccc', dict_with_coding)) print(code_string('babcac', dict_with_coding)) print(code_string('cabcbab', dict_with_coding))