[Solved]Problem in Dictionary | SoloLearn: Learn to code for FREE!

+5

[Solved]Problem in Dictionary

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

5/29/2020 6:55:54 AM

Aronya Biswas

3 Answers

New Answer

+7

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.

+5

Since there is only one input you can only input one letter

+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