[Solved]Problem in Dictionary | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

[Solved]Problem in Dictionary

When input is more than one letter, it shows Error. https://code.sololearn.com/cY2HlrSKoWd5/?ref=app

29th May 2020, 6:55 AM
Aronya Biswas
Aronya Biswas - avatar
3 Answers
+ 8
Your dict has all keys as single letters. If you input more than 1 letter, you will get a Key error, because there is no match from your input to the keys in dict. If you are going to use <dict>. get(), you will get no Key error. You get None as a retur if there is no match. print(playfair.get(new_inp)) But you can customize what kind of feedback you want to get: print(playfair.get(new_inp,'key not found')) In this case 'key not found' will be retured if no match from input to keys in dict will be found.
29th May 2020, 11:02 AM
Lothar
Lothar - avatar
+ 6
Since there is only one input you can only input one letter
29th May 2020, 7:01 AM
Abhay
Abhay - avatar
+ 1
You can take the input in the form of a list and then apply the dictionary substitution in each letter using for loop.... x = list(input()) for i in range(len(x)): print(dic[x[i]]) Try this if you want them in just one line then use function generator(using yield).... And then use join function... Make a key of ' ' space also so that your code can even take a sentence
29th May 2020, 7:01 AM
Namit Jain
Namit Jain - avatar