def find_acronym():
# if search term in database returns acronym and expansion
for abbr, text in acronyms.items():
if abbr == search_analyte.get():
expansion.insert(0.0,'{0:>6}: {1: <10}\n'.format(abbr, text))
elif str(search_analyte.get()) in text:
expansion.insert(0.0,'{0:>6}: {1: <10}\n'.format(abbr, text))
# if search term not in database , returns message DOES NOT WORK PROPERLY!
if search_analyte.get() not in text or abbr != search_analyte.get():
expansion.insert(0.0,'"{0}"{1} \n {2}\n'.format(search_analyte.get(),' is not in the database.','Add,if appropriate'))
我使用此函数以{ ACRONYM: text details, ACRONYM2: its test,...}
算法的工作原理是它检索任何搜索项的首字母缩略词和文本,但总是返回来自最后一条条件的文本消息,用于发现项目是否在数据库与否。我显然不合逻辑,或者我不明白循环如何在Python中工作。
答案 0 :(得分:1)
现在的方式,最后一个测试是在 FOR循环完成后运行,因此您无法再与abbr
和text
进行比较
你想要这样的东西:
def find_acronym():
found = False
# if search term in database returns acronym and expansion
for abbr, text in acronyms.items():
if abbr == search_analyte.get():
expansion.insert(0.0,'{0:>6}: {1: <10}\n'.format(abbr, text))
found = True
elif str(search_analyte.get()) in text:
expansion.insert(0.0,'{0:>6}: {1: <10}\n'.format(abbr, text))
found = True
# if search term not in database
if not found:
expansion.insert(0.0,'"{0}"{1} \n {2}\n'.format(search_analyte.get(),' is not in the database.','Add,if appropriate'))
答案 1 :(得分:1)
如果我理解你想要达到的目标,可以采用以下方法:
def find_acronym():
found=False
# if search term in database returns acronym and expansion
for abbr, text in acronyms.items():
if abbr == search_analyte.get():
found=True
expansion.insert(0.0,'{0:>6}: {1: <10}\n'.format(abbr, text))
elif str(search_analyte.get()) in text:
found=True
expansion.insert(0.0,'{0:>6}: {1: <10}\n'.format(abbr, text))
# if search term not in database , returns message DOES NOT WORK PROPERLY!
if not found:
expansion.insert(0.0,'"{0}"{1} \n {2}\n'.format(search_analyte.get(),' is not in the database.','Add,if appropriate'))