0
Help!! Why is count variable unknown?
The description is in the code https://code.sololearn.com/cJeXNWjnx7km/?ref=app
4 Respostas
+ 1
Here is the code that will work:
sentence = "This is a test...Just a test"
def find(phrase):
word = 'es'
count = 0
word_len = len(word)
#loop till end of the sentence
for i in range(len(sentence)):
#loop for every two words(word_len) in sentence and check for same word
if sentence[i:i+word_len] == word:
count += 1
return count
print(find(sentence) )
0
Count variable is a local variable of the function/method. It cannot be accessed outside the find method. Moreover you are using unnecessary return statements in if else blocks
0
Prince so how can I fix it?
And there are no return statement in else block...but a local variable can be accessed with a return statement, no?
0
THX mate... nice trick...and I think that I have another solution ( still testing it)
Have a nice day Prince!