Q:

Recursive return

def algae(S, n):
    """
    Print S rewritten with the algae rule to recursion depth n
    """
    al = {'A': 'AB', 'B': 'A'}
    # Base case
    if n == 0:
        return S
    # Transform each symbol in S
    for symbol in S:
        S += algae(al[symbol], n - 1)

print(algae('A', 5))

Hi, can anyone explain why when printing the outcome of this function I am receiving the error:
TypeError: Can't convert 'NoneType' object to str implicitly

This is referring to line 11 (S += algae(al[symbol], n - 1))

A:

When n != 0, your code falls off the end of the function. In Python, that's equivalent to returning None. You need to add a return statement for the recursive case.

A:

Edit:
This is a working version of your script:
def algae(S, n):
    """
    Print S rewritten with the algae rule to recursion depth n
    """
    al = {'A': 'AB', 'B': 'A'}
    if n == 0:
        return S
    # Make a new string to build on
    mystr = ""
    for symbol in S:
        # Add the translation to the new string
        mystr += al[symbol]
    # Recursively call the function, passing in the string
    return algae(mystr, n-1)

print(algae('A', 5))

Output:
ABAABABAABAAB

Note: If you want, you can make this a lot more efficient like  @Blckknght said:
def algae(S, n):
    """
    Print S rewritten with the algae rule to recursion depth n
    """
    al = {'A': 'AB', 'B': 'A'}
    if n == 0:
        return S
    mystr = "".join(al[c] for c in S)
    return algae(mystr, n-1)

print(algae('A', 5))

