Python if-else nested instructions

I have an instruction like this

for word in tweet_text: if word in new_words: if new_words[word] == 0: new_words[word] = sent_count else: new_words[word] = (new_words[word] + sent_count) / 2 

And I am very suspicious that the else block is executed every time the first condition is not satisfied ( if word in new_words ), is this possible? Am I doing something wrong with identification?

+4
source share
1 answer

The else clause matches an if at the same level of indentation as expected.

The problem you see may be due to the fact that you are mixing tabs and spaces, so the apparent level of indentation is different from what your interpreter sees.

Change all tabs to spaces and check if the problem persists.

+9
source

Source: https://habr.com/ru/post/1479818/


All Articles