簡體   English   中英

具有比較運算符的if語句在另一個if語句中未運行

[英]If statement with comparison operators inside another if statement not running

我是Python的新手。 我正在嘗試建立一個規則,如果同時滿足兩個條件,程序將生成一個變量,但是由於某種原因, if該規則的語句未運行。

讓我解釋一下我正在做的事情的上下文:我有以下列表和一個函數,該函數返回與任何兩個給定列表,字典,字符串等匹配的元素:

poa_term_variations = ['power of attorney', 'poa']
poa_corporate_identifier = ["to attend any partners’ meeting", 'to represent the grantor regarding any change or amendment to the articles of association', "to subscribe for new quotas of the company's capital"]
poa_cnpj_identifier = ['brazilian federal revenue office', 'basic cnpj entry document']


#text variable is actually information from a .txt file in my code, which I converted into a list just like below. 
text = ['da#897-0095-v4',
 'd#30/04/2019',
 'h#2.0',
 'power of attorney',
 '(a) to represent the grantor in its capacity of partner of the limited
 liability company hk co., with head office in xxxxx, enrolled with the 
general taxpayers registry (cnpj/mf) under no. xxxxx and with the registry of 
companies under no. xxxxx (hereinafter referred to as company); (b) to attend any 
partners meeting of the company and vote the quotas of the grantor 
as instructed by the grantor, by email sent from mr. [complete name], in relation 
to any matter submitted to the appreciation of the partners, including, but not limited 
to, the approval of financial statements and election of managers, officers
 and/or directors; (c) to represent the grantor regarding any change or amendment 
to the articles of association approved by the grantor; (d) to subscribe for new quotas of the company\'s capital approved by the grantor']


#this function verifies if a certain term inside a list is also located in another list
def term_tracker(document, term_variations):
    terms = []         

    #If term_variations is a list
    if isinstance(term_variations, list) == True:
        for term in term_variations:
            #If we find a term in the document, append that term to a list
            if any([str(term) in i for i in document]):
                terms.append(term)

    #If term_variations is a string, find that string in all documents
    elif isinstance(term_variations, str) == True:
        if any([term_variations in i for i in document]) == True:
            terms.append(term_variations)

    return terms

由於某種原因,每當我嘗試傳遞以下代碼塊時,第一個elif語句都不會運行:

for string in text:
        if len(term_tracker(text[0:4], poa_term_variations)) > 0:
            print('Found PoA type')
            document_type = term_tracker(text, poa_term_variations)

            if len(term_tracker(text, poa_corporate_identifier)) > 0:
                if len(term_tracker(text, poa_cnpj_identifier)) > 0:
                    document_nature = 'granting powers for corporate representation and representation before the Federal Revenue Office'
                    print('Found PoA Corporate/CNPJ type')
                    break

            #THIS IS THE STATEMENT THAT SHOULD RUN AND IT IS NOT RUNNING                                                                
            elif len(term_tracker(text, poa_corporate_identifier)) > 0:
                if len(term_tracker(text, poa_cnpj_identifier)) == 0:
                    document_nature = 'granting powers for corporate representation'
                    print('Found PoA Corporate type')
                    break

            elif len(term_tracker(text, poa_cnpj_identifier)) > 0:
                print('ok1')
                if len(term_tracker(text, poa_corporate_identifier)) == 0:
                    print('ok2') 
                    document_nature = 'granting powers for representation before the Federal Revenue Office'
                    print('Found PoA CNPJ type')             

            work_summary = ['Work description: ' + 'Drafting PoA for the purposes of ' + str(document_nature) + '.']

我知道第一個if語句將運行,因為print('Found PoA type')將運行。 但是,第一個elif語句也應運行,因為(i) poa_corporate_identifier列表在text變量中至少包含一個術語匹配,並且(ii) poa_cnpj_identifiertext變量中沒有任何術語匹配。 這是我得到的錯誤:

>>> work_summary = ['Work description: ' + 'Drafting PoA for the purposes of ' + str(document_nature) + '.']
>>> NameError: name 'document_nature' is not defined 

請注意,我正在使用其他示例文檔來測試我的代碼,這些示例文檔與第二條if語句和第二條elif語句中的條件匹配,可以正常運行。

已經嘗試了其他比較運算符( !=<=is not等),但沒有成功。

我該如何解決這個問題?

條件是,所述表達elif語句是完全一樣的一個在所述第一if語句( len(term_tracker(text, poa_corporate_identifier)) > 0 ),要么只有if語句將運行,或者任何其他elif / else支票對於不同的條件。

假設給定相同的參數時term_tracker返回相同的結果,因此對所有AB來說term_tracker(A, B) == term_tracker(A, B)

elif表示else if ,如果第一個條件為true,則它將不運行。 ifelif條件相等,則永遠不會達到第二個條件。

另外,您正在運行for string in text:循環中的for string in text:並且僅在其中使用text 您可能應該使用string代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM