是否可以输入多个单词字符串来搜索多个匹配的字典键? -谢谢。
def concept(word):
# use this to list python file titles and links to open them in a new tab
files = {1:"http://file_title0001.py",
2:"file_title0002.txt",
3:"file_title0003.txt",
4:"file_title0004.txt",
5:"file_title0005.txt",
6:"file_title0006.txt",
7:"file_title0007.txt",
8:"file_title0008.txt",
9:"file_title0009.txt"}
# change keys to searchable simple keyword phrases.
concepts = {'GAMES':[1,2,4,3,3],
'BLACKJACK':[5,3,5,3,5],
'MACHINE':[4,9,9,9,4],
'DATABASE':[5,3,3,3,5],
'LEARNING':[4,9,4,9,4]}
if word.upper() not in concepts:
print("Nothing Found in Database")
return
for pattern in concepts[word.upper()]:
print(files[pattern])
while True:
concept(input("Enter Concept Idea: "))
print("\n")
答案 0 :(得分:2)
我想这就是你想要的:
def print_big(word):
# use this to list python file titles and links to open them in a new tab
files = {1:"http://file_title0001.py",
2:"file_title0002.txt",
3:"file_title0003.txt",
4:"file_title0004.txt",
5:"file_title0005.txt",
6:"file_title0006.txt",
7:"file_title0007.txt",
8:"file_title0008.txt",
9:"file_title0009.txt"}
# change keys to searchable simple keyword phrases.
concepts = {'GAMES':[1,2,4,3,3],
'BLACKJACK':[5,3,5,3,5],
'MACHINE':[4,9,9,9,4],
'DATABASE':[5,3,3,3,5],
'LEARNING':[4,9,4,9,4]}
if word.upper() not in concepts:
print("Nothing Found in Database")
return
for pattern in concepts[word.upper()]:
print(files[pattern])
print_big(input("Enter Concept Idea: "))
答案 1 :(得分:0)
您可以使用try/except
子句来解决不存在密钥(KeyError
)的情况:
try:
for pattern in concepts[word.upper()]:
print(files[pattern])
except KeyError as err:
print("Nothing Found in Database for '{}'".format(err))