Reputation: 465
it asks the user to enter a class name. but prints not found
myDict={"John":["Maths261,"Econ120"],"Mathew":["CSIS256,"Econ120"]}
classFind=input("Enter Name to find class:")
for key in myDict:
if classFind in key:
tmpVal=myDict[key]
print(tmpVal)
else:
print("Not found")
Upvotes: 1
Views: 56
Reputation: 226
myDict = {"John":["Maths261","Econ120"],"Mathew":["CSIS256","Econ120"]}
classFind = raw_input("Enter Name to find class:")
if classFind in myDict.keys() :
print myDict[classFind]
else :
print "Not Found"
If you enter someone's name you will be returned their courses.
Upvotes: 0
Reputation: 2567
myDict={"John":["Maths261","Econ120"],"Mathew":["CSIS256","Econ120"]}
classfind = raw_input('enter classname : ')
for name, classes in myDict.iteritems():
if classfind in classes:
print name
output:
enter classname : Econ120
Mathew
John
Upvotes: 0
Reputation:
Assuming you are searching names as opposed to classes, you could replace the for
loop with
tmpVal = myDict.get(classFind, "Not found")
print(tmpVal)
which I believe follows the hint in the comment to your question.
So if the value of classFind
is a key in your dictionary, its value will be printed. Otherwise it will print "Not found"
.
Upvotes: 1