+ 1
#dictionary d={"mayukh":"9/11/2005", "puneet":"10/11/2005"} word=(input ("enter your word")) print (d (word))
Can anyone tell me the mistake in the program ?? https://code.sololearn.com/c2gB6FiLBIzL/?ref=app
6 odpowiedzi
+ 9
Puneet Kumar ,
see your code slightly modified, see also the comments.
#dictionary
d={"mayukh":"9/11/2005", "puneet":"10/11/2005"} # we need to turn your first line into 2 lines
word=(input ("enter your word: ")) # inserted a space + : at the end of the string
if word in d: # add this to make sure that the input string can be found as a key in the dictionary
print(d[word])
+ 6
You code is incomplete/ the indentation is messed up.
Please DO NOT put code in the title section. It gets cut off.
LINK your code instead.
+ 6
Please link your code so we can test it and look for the issue.
+ 4
Use square brackets to get value by key from dictionary.
Wrong : print(d(word))
Correct : print(d[word])
+ 4
Like Aditya said you can use the square bracket to do it, but if you use the square bracket when it won't find the key, it will return a key error.
Instead, you can use the .get() method to get the desired input if the key doesn't exist then it by default returns None instead of a key error.
You can also set the default value in the second parameter in the get method().
An example:
https://code.sololearn.com/cv790Op6fmcm?ref=app
0
Ok