Jul-19-2020, 01:54 PM
(This post was last modified: Jul-19-2020, 06:34 PM by deanhystad.)
break takes you out of the inner loop, but you are still stuck in the outer loop. An easy way to break out of both is make this a function and use return.
def get_word_count(): count = {} for line in sys.stdin: for word in line.split(): if word == 'submit': return count count[word] = count.get(word,0) + 1 for key,value in get_word_count().items(): print(key,value)I see this a lot and it makes me wonder where it comes from. There is no reason to for the else in this code:
if word == 'submit': break else: # Does nothing! dictionary_words[word] = dictionary_words.get(word,0) + 1If the word is 'submit' it will break out of the loop and not update the word count. If the word is not submit it will update the word count in the dictionary. The code does the same thing with or without the else.