它要求用户输入一个类名。但没有找到打印件
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")
答案 0 :(得分:1)
假设您正在搜索名称而不是类,您可以用
替换for
循环
tmpVal = myDict.get(classFind, "Not found")
print(tmpVal)
我相信在你的问题评论中提示。
因此,如果classFind
的值是字典中的键,则会打印其值。否则它将打印"Not found"
。
答案 1 :(得分:0)
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"
如果您输入某人的姓名,您将返回他们的课程。
答案 2 :(得分:0)
myDict={"John":["Maths261","Econ120"],"Mathew":["CSIS256","Econ120"]}
classfind = raw_input('enter classname : ')
for name, classes in myDict.iteritems():
if classfind in classes:
print name
输出:
enter classname : Econ120
Mathew
John