它要求用户输入类名。但找不到打印
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")
最佳答案
假设您正在搜索名称而不是类,您可以将 for
循环替换为
tmpVal = myDict.get(classFind, "Not found")
print(tmpVal)
我相信这遵循了您的问题评论中的提示。
因此,如果 classFind
的值是字典中的键,则会打印其值。否则,它将打印“未找到”
。
关于python - 从字典中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29022142/