"""For loops: Accumulator and Search patterns"""

def count_letter(word: str, letter: str) -> int:
    """Return the number of times letter appears in word.
    
    >>> count_letter("Hello", "l")
    2
    >>> count_letter("Hello", "b")
    0

    Precondition: len(letter) == 1  
    """

    # Accumulator pattern:
    #  1. Set a variable outside the for loop as the accumulator.
    #  2. Change the accumulator in the for loop body as needed.
    #  3. When done looping, return the accumulator variable.
    #       (or continue using it in your function)
    
    # Variable count acts as a numerical accumulator
    # count represents the number of times letter appears in word
    #   and it starts off at 0.
    count = 0
    
    # now we want to loop through the string and add to our accumulator
    for char in word:
        if char == letter:
            #print(char)
            # Increment the accumulator by 1 if char is the same
            # as letter
            count = count + 1
    # return indented outside of the for loop
    return count    
            

print(count_letter("Hello", "l"))


# A boolean function - tells us about some property of the string.
# tells us whether or not the string has a certain property.
def is_IP_address(address: str) -> bool:
    """Return True iff address contains only digits and periods.

    >>> is_IP_address('255.14.128.1')
    True
    >>> is_IP_address('40 St. George St')
    False
    """

    # Search pattern:
    #  1. Loop through the string and look for a certain property
    #      (decide whether it should have or not have that property)
    #  2. Return immediately once you've found out.
    
    for ch in address:
        # the property we're checking is whether ch is a
        # digit or a period.
        if not (ch.isdigit() or ch == '.'):
            return False
        # wrong - would only give us first character:
        #else: 
            #return True
            
        # also wrong: we're still in the for loop
        #return True
    return True
