AllenDowney / ThinkPython2

LaTeX source and supporting code for Think Python, 2nd edition, by Allen Downey.
Other
2.49k stars 1.65k forks source link

Exercise 9.7 cartalk1.py #81

Closed IvansNab closed 3 years ago

IvansNab commented 3 years ago

My code finds 46 words in words.txt but your code finds 4 words. Why is it like that? Here's my code:

def is_three_consecutive_double(word):
    """Tests if a word contains three consecutive double letters

    Args:
        word (str): ASCII letters

    Returns:
        bool: True or False
    """    
    c = 0
    while len(word) > 1:
        if word[0] == word[1]:
            c += 1
        word = word[1:]
    return True if c == 3 else False

def main():
    """Reads words.txt with English words
       Prints a word list and count 
    """    
    f = open('words.txt')
    words_list = f.read().split('\n')
    words_list.remove('')
    f.close()
    consecutive_double = []
    count = 0

    for word in words_list:
        if is_three_consecutive_double(word):
            consecutive_double.append(word)
            count += 1

    print(consecutive_double, count)

if __name__ == '__main__':
    main()

I use VS code 1.55.0 on Windows 10. Thanks

IvansNab commented 3 years ago

NVM i found my misstake :-)