0
模糊匹配,我有以下解釋:Python的字典上的按鍵
classes = {'MATH6371': 'Statistics 1', 'COMP7330': 'Database Management',
'MATH6471': 'Statistics 2','COMP7340': 'Creative Computation' }
,我試圖做的字典鍵的raw_input一個模糊匹配。例如,如果我在「數學」輸入,輸出會統計1統計2
我有以下的代碼,但它只有完全匹配的密鑰:
def print_courses (raw_input):
search = raw_input("Type a course ID here:")
if search in classes:
print classes.get(search)
else:
print "Sorry, that course doesn't exist, try again"
print_courses(raw_input)
感謝
高清print_courses(的raw_input): 搜索的raw_input =( 「在此處鍵入課程ID」) 如果類搜索: 如果在key.lower()中搜索的話,search = [classes [key] for key in classes else: print「Sorry,that course does not exist,try again」 print_courses(raw_input)得到的是「對不起,當然不存在,請重試」 – Andrew
@Andrew擺脫'if語句',檢查返回的數組是否爲空。 – Sait
我刪除了if語句,然後IDE提示我刪除else語句。當我運行腳本時,什麼都不會輸出。 – Andrew