Home > Back-end >  Print "Lexical, Syntactic" using if else statement in Python
Print "Lexical, Syntactic" using if else statement in Python

Time:05-20

I'm new on Python, I have problem to print "Lexical, Syntactic" in my elif statement. The result shows "Lexical" even in the str have both Lexical and Syntactic words. There's something wrong with my elif statement and already tried ways to recover it but still haven't get it right.

It only print "Lexical", should be it prints "Lexical, Syntactic" because in str there are 'and' (from syntactic) and 'reference' (from lexical)

Thank you in advance for your helps :)

lexical = [' bound ', ' break ',' call ', ' content ', ' continue ', ' contract ', ' count ', ' direct ', ' even ', ' express ', ' form ', \
          ' forward ', ' function ', ' get ', ' job ', ' level ', ' name ', ' notice ', ' number ', ' out ', ' part ', ' position ', ' record ', \
          ' reference ', ' return ', ' set ', ' source ', ' special ', ' standard ', ' string ', ' subject ', ' switch ', ' tail ', ' throw ', \
          ' translate ', ' try ', ' under ', ' value ', ' way ']

syntactic = [' and ', ' but ', ' or ', ' so ', ' because ', ' however ', ' after ', ' since ', ' during ', ' then ', ' unless ', ' that ', ' while ', \
             ' also ', ' if ', ' only if ', ' if then ', ' for ', ' just ', ' just as ', ' neither ', ' nor ', ' either ', ' not only ', ' whether ', \
             ' yet ', ' once ', ' whenever ', ' while ', ' although ', ' not only ', ' but also ', ' before ', ' as much as ', ' as soon as ', \
             ' as long as ', ' as though ', ' though ']

semantic = [' all ', ' some ', ' more ', ' a lot of ', ' enough ', ' any ', ' most ', ' lots of ', ' less ', ' many ', ' not many ', ' each ', ' a few ', \
            ' least ', ' several ', ' both ', ' fewer ', ' a little ', ' little ', ' very little ', ' a bit ', ' large amount ', ' large quantity ', ' much ', \
            ' none ', ' not any ', ' a lot of ', ' plenty of ', ' a number ', ' large number ', ' majority of ', ' great number of ']
pragmatic = [' I ', ' he ', ' she ',' it ', ' me ', ' her ', ' him ', ' them ', ' hers ', ' his ', ' its ', ' your ', ' their ', ' our ', ' herself ', ' himself ', \
             ' itself ', ' ours ', ' ourselves ', ' yourself ', ' themselves ', ' yourselves ', ' that ', ' theirs ', ' these ', ' they ', ' this ', ' which ', \
             ' who ', ' you ', ' yours ',' anyone ', ' everyone ', ' somebody ', ' anybody ', ' everybody ', ' something ', ' anything ', ' everything ']


str = "one two three four five and reference "


if any(x in str for x in lexical):
    print ("Lexical")
elif any(x in str for x in syntactic):
    print ("Syntactic")
elif any(x in str for x in semantic):
    print ("Semantic")
elif any(x in str for x in pragmatic):
    print ("Pragmatic")
elif any(x in str for x in lexical and syntactic):
    print('Lexical, Syntactic')
else:
    print ("Clean")

CodePudding user response:

Only the first match in your chain of if/elif statements will be executed. Others will be checked only if the previous statements do not evaluate to true (hence the name else if). In your case, this first match happens immediately at the first statement, so the one you expect it to match is ignored.

Simple solution

The simple solution would be to move this case up to the top, and order your elif statements from most to least specific:

if any(x in str for x in lexical and syntactic):
    print('Lexical, Syntactic')
elif any(x in str for x in lexical):
    print ("Lexical")
elif any(x in str for x in syntactic):
    print ("Syntactic")
elif any(x in str for x in semantic):
    print ("Semantic")
elif any(x in str for x in pragmatic):
    print ("Pragmatic")
else:
    print ("Clean")

Better solution

Although this produces the right result for your question, it will quickly become difficult to maintain, since there are a lot of combinations possible. A simple way to check for words in each of the lexical/syntactic/semantic/pragmatic lists is like so:

lexical = [' bound ', ' break ',' call ', ' content ', ' continue ', ' contract ', ' count ', ' direct ', ' even ', ' express ', ' form ', \
          ' forward ', ' function ', ' get ', ' job ', ' level ', ' name ', ' notice ', ' number ', ' out ', ' part ', ' position ', ' record ', \
          ' reference ', ' return ', ' set ', ' source ', ' special ', ' standard ', ' string ', ' subject ', ' switch ', ' tail ', ' throw ', \
          ' translate ', ' try ', ' under ', ' value ', ' way ']

syntactic = [' and ', ' but ', ' or ', ' so ', ' because ', ' however ', ' after ', ' since ', ' during ', ' then ', ' unless ', ' that ', ' while ', \
             ' also ', ' if ', ' only if ', ' if then ', ' for ', ' just ', ' just as ', ' neither ', ' nor ', ' either ', ' not only ', ' whether ', \
             ' yet ', ' once ', ' whenever ', ' while ', ' although ', ' not only ', ' but also ', ' before ', ' as much as ', ' as soon as ', \
             ' as long as ', ' as though ', ' though ']

semantic = [' all ', ' some ', ' more ', ' a lot of ', ' enough ', ' any ', ' most ', ' lots of ', ' less ', ' many ', ' not many ', ' each ', ' a few ', \
            ' least ', ' several ', ' both ', ' fewer ', ' a little ', ' little ', ' very little ', ' a bit ', ' large amount ', ' large quantity ', ' much ', \
            ' none ', ' not any ', ' a lot of ', ' plenty of ', ' a number ', ' large number ', ' majority of ', ' great number of ']
pragmatic = [' I ', ' he ', ' she ',' it ', ' me ', ' her ', ' him ', ' them ', ' hers ', ' his ', ' its ', ' your ', ' their ', ' our ', ' herself ', ' himself ', \
             ' itself ', ' ours ', ' ourselves ', ' yourself ', ' themselves ', ' yourselves ', ' that ', ' theirs ', ' these ', ' they ', ' this ', ' which ', \
             ' who ', ' you ', ' yours ',' anyone ', ' everyone ', ' somebody ', ' anybody ', ' everybody ', ' something ', ' anything ', ' everything ']


str = "one two three four five and reference "

output_string = ''

if any(x in str for x in lexical):
    output_string  = "Lexical, "
if any(x in str for x in syntactic):
    output_string  = "Syntactic, "
if any(x in str for x in semantic):
    output_string  = "Semantic, "
if any(x in str for x in pragmatic):
    output_string  = "Pragmatic, "

if output_string == '':
    output_string = 'Clean'

print(output_string)

Here, we create an output string which will contain all of the matching categories and append to that string each time we find a match. Note the use of if statements instead of elif, so that each case is checked and not ignored when a match is found. At the end, we check if the output string is empty (in which case no match has been found) and we set the output string to 'Clean'.

  • Related