Home > Software design >  Number doesn't increased by nothing after for loop
Number doesn't increased by nothing after for loop

Time:06-02

I created a program that enters Nytimes and search School Shooting and then I tried to search how many times the program found the word 'Shooting' and count the times the program found the word. I wont send the selenium part only the part that I struggle with. If you have any suggestions I would be thankful.

Here's the code:

string1 = "Shooting"
index = 0
num = 0
file=open('Information.txt', 'r')

for word in file:
    index  1
    num 1
    if string1 in word:
        print("I Found",string1)
        print(num)

CodePudding user response:

Incrementing is done using the = operator, not the operator. I believe this fix should make it work:

string1 = "Shooting"
index = 0
num = 0
file=open('Information.txt', 'r')

for word in file:
    index  = 1
    num  = 1
    if string1 in word:
        print("I Found",string1)
        print(num)

EDIT: @TechGeek makes a good point about where you are actually incrementing the variables; num would probably want to be within the if check.

CodePudding user response:

It should be written as:

Index  =1
  • Related