Help!! Why is count variable unknown? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help!! Why is count variable unknown?

The description is in the code https://code.sololearn.com/cJeXNWjnx7km/?ref=app

20th Apr 2020, 2:47 PM
YoungMind
YoungMind - avatar
4 Answers
+ 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) )
20th Apr 2020, 3:21 PM
Prince
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
20th Apr 2020, 2:55 PM
Prince
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?
20th Apr 2020, 2:56 PM
YoungMind
YoungMind - avatar
0
THX mate... nice trick...and I think that I have another solution ( still testing it) Have a nice day Prince!
20th Apr 2020, 3:32 PM
YoungMind
YoungMind - avatar