q, I already asked a similar question, but again there was a similar problem. I have the following code:

input_text = str(input('Введите ваш текст: ')) list1 = ['3','10','2'] list2 = ['а', 'б', 'в'] vivod = [] for c in input_text: if c in list1: i = list1.index(c) vivod.append(list2[i]) print(' '.join(vivod)) 

The code asks for the user to enter a number and when he types in, for example, 3 looks for it in the first list and displays this index from the second, but if you enter a two-digit number, for example, 10 in the console will display an error or if it will find the number 1 in the list will display its index in the answer. I need to (using this code as an example) when entering 10, output its index from the second, and not index 1.

P'S

Sorry for the mistakes and not the normally posed question, please do not kick your feet.

  • one
    What is the meaning of the for c in input_text ? - andreymal Nov.

2 answers 2

Something like this...

 list1 = ['3','10','2'] list2 = ['а', 'б', 'в'] vivod = [] input_text = [str(x) for x in input('Введите ваш текст: ').split()] for data in input_text: if data in list1: vivod.append(list2[list1.index(data)]) print(' '.join(vivod)) 
  • If you need to enter several characters at once? - HIPOL
  • Corrected. Now you can enter several values ​​separated by a space, if you need another separator, then replace it with the split () method. I hope I understood correctly what you need. :) - Ins

Just a solution in a more Python style :)

 input_text = str(input('Введите ваш текст: ')) list1 = ['3','10','2'] list2 = ['а', 'б', 'в'] s_dic = dict(zip(list1, list2)) vivod = [s_dic[str] for str in input_text.split(" ") if str in s_dic] print(' '.join(vivod))