When Python reads the English text file and sorts the sentences according to the keywords, it does not read the list in the code.

Asked 2 years ago, Updated 2 years ago, 17 views

I'm a beginner studying Python.
I've been practicing a program to read the English sentences in the text file with the following code and sort them out as statements, but the second line list with keywords for sorting doesn't seem to work well in the elif syntax of line 21.
If you don't mind, please let me know why it doesn't work and how it works properly.

Contents of the text file used

Strawberries are fruit.
Do you like scrap jelly?
The Church is true.
She sings well.
2 + 2 = 5
how are you doing
Go.
School is located in Japan

Code Contents

 from sys import argv


qWord=["what", "where", "how", "why", "who", "when", "he", "she", "it", "they" ]

iflen(argv) == 1:
    print("Type argument/filename.") 
else:
    filename = argv[1]
    try:
        with open(filename) as file:
            contents=file.read()
    except IOError:
        print("No File Found")
    else:

        file=open(filename)
   # process to open the file


        For line in file: 
            line = line.rstrip()

            ifline [-1] == "?":
                print(line+"NOT A STATEMENT")

            elif" "not in line: 
                print(line+"NOT A STATEMENT")

            elif line in qWord:
                print(line+"NOT A STATEMENT")
            else:
                print(line+"STATEMENT")

Reaction to the original intent within the command line

Strawberries are fruit.STATEMENT
Do you like graphic jelly? NOT A STATEMENT
The Church is true.STATEMENT
Sheings well.NOTA STATEMENT
2 + 2 = 5 STATEMENT
How are you doing NOT A STATEMENT
Go. NOT A STATEMENT
School is located in Japan STATEMENT

Actual Response

Strawberries are fruit.STATEMENT
Do you like graphic jelly? NOT A STATEMENT
The Church is true.STATEMENT
Sheings well.STATEMENT
2 + 2 = 5 STATEMENT
How are you doing STATEMENT
Go. NOT A STATEMENT
School is located in Japan STATEMENT

python

2022-09-30 14:31

1 Answers

Based on the advice you gave me, I will list the code that worked fine below.
Thank you for your help!

 from sys import argv

questionSig='?'
space = ' '
qWord=["what", "where", "how", "why", "who", "when", "he", "she", "it", "they" ]

iflen(argv) == 1:
    print("Type argument/filename.") 
else:
    filename = argv[1]
    try:
        with open(filename) as file:
            contents=file.read()
    except IOError:
        print("No File Found")
    else:

        file=open(filename)
   # process to open the file



        For line in file: 
            line = line.rstrip()


            if any (word in line for word in qWord):
                print(line+"NOT A STATEMENT")


            elif" "not in line: 
                print(line+"NOT A STATEMENT")

            elifline [-1] == "?":
                print(line+"NOT A STATEMENT")
            else:
                print(line+"STATEMENT")


2022-09-30 14:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.