DC930
DC930

Reputation: 1

Can't tell why I'm getting "TypeError: Can't convert 'int' object to str implicitly" error

I'm teaching myself to code so forgive me if the answer is obvious or my code is a wreck. I tried putting together a simple search engine that counts the number of times a word appears, but I keep getting the above error message so I can't even test it. Any idea what I'm doing wrong?

def search(text_body, phrase):
    count = 0
    word_length = len(phrase)
    for i in text_body:
        if phrase == text_body[i:i+word_length]:
            count +=1 
    return count

text_body = "text text text text text"
phrase = input("Search for: ")
final_count = search(text_body, phrase)

print(final_count)

Edit: Apologies, the full error message is here:

Traceback (most recent call last):                                                                                                             
  File "main.py", line 21, in <module>                                                                                                         
    final_count = search(text_body, phrase)                                                                                                    
  File "main.py", line 14, in search                                                                                                           
    if phrase == text_body[i:i+word_length]:                                                                                                   
TypeError: Can't convert 'int' object to str implicitly 

Upvotes: 0

Views: 51

Answers (3)

SnakeException
SnakeException

Reputation: 1186

When python executes

for i in text_body:
    if phrase == text_body[i:i + word_length]:
        count += 1 

in the definition of search the value i picks up each element, instead of the index, and thus i + word_length throws the TypeError. Here is the solution, using range() instead of for i in text_body.

for i in range(len(text_body)):
    if phrase == text_body[i:i + word_length):
        count += 1

Upvotes: 0

Chi
Chi

Reputation: 322

Others have already given a good explanation on why your code is breaking and a good fix, but to demonstrate this we can look at a simple for loop:

text_body = "text"
for i in text_body:
    print(i)

Which prints:

t
e
x
t

You can see from this that in the snippet:

text_body[i:i+word_length]

you're trying to do:

text_body['t':'t'+5]

Which is why Python is getting confused, since you're trying to add a string to an int, hence the error.

Something really important to note is that Python strings actually have a method for exactly what you're doing already:

>>> "text text text text text".count("text")
5

Or, for your case:

text_body.count(phrase)

Upvotes: 1

DYZ
DYZ

Reputation: 57033

The value of i in for i in text_body: is the next character from text_body (a one-character string). You cannot add a number and a string in i+word_length. You should iterate through the indexes:

for i in range(len(text_body)):

Better yet, use the function text_body.index.

Upvotes: 0

Related Questions