1
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,...}

このアルゴリズムは、任意の検索項目の頭字語とテキストを取得するという意味で機能しますが、項目がデータベースにあるかどうかを検出することを意図した最後の if 条件からのテキスト メッセージも常に返します。私は明らかに非論理的であるか、Python でループがどのように機能するかを理解していません。

4

2 に答える 2

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'))
于 2013-05-08T10:30:55.953 に答える
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'))
于 2013-05-08T10:26:49.570 に答える