0

I'm working on a set of code where I have to turn a loop into a recursive function and the professor has not been able to help nor my friends I've reached out to. The loop function is this:

 def __str__(self):
     """ Returns a string representation of the list with a space between each item.
         Precondition:  none
         Postcondition:  Returns a string representation of the list with a space between each item.
     """
     resultStr = "(head)"
     current = self._head
     while current != None:
         resultStr += " " + str(current.getData())
         current = current.getNext()
     return resultStr + " (tail)"

The recursive function I have typed so far is this:

def __str__(self):
        """ Returns a string representation of the list with a space between each item.
            Precondition:  none
            Postcondition:  Returns a string representation of the list with a space between each item.
        """
        
        def strHelper(current):
            if current != None:
                str(current.getData()) + " " + strHelper(current.getNext())
            else:
                return ""
        
        # Start of __str__ method execution by passing pointer to first node in list
        return "(head) " + strHelper(self._head) + "(tail)"

The professor basically said, "This should work!" but I still get a TypeError reading "can only concatenate str (not "NoneType") to str" on the final return line. What do I do?

4
  • 1
    Dont do "something" + None + "some other thing" ... check if the things in your stringconcattenation are None and avoid concatting them in that case... Commented Jun 25, 2020 at 13:27
  • 1
    how-to-debug-small-programs/ Commented Jun 25, 2020 at 13:27
  • Paste your full code in pythontutor.com/visualize.html#mode=edit and step through the code to see where you have a None value where you expect a string. Commented Jun 25, 2020 at 13:36
  • 1
    Your if case in strHelper doesn't do anything, since you forgot the return keyword. strHelper returns either None (after computing and discarding a str value) or the empty string. Commented Jun 25, 2020 at 13:42

1 Answer 1

1

You forgot the return keyword in your helper function.

def __str__(self):
        
    def strHelper(current):
        if current is not None:
            return str(current.getData()) + " " + strHelper(current.getNext())
        else:
            return ""
        
    return "(head) " + strHelper(self._head) + "(tail)"

I would consider replacing recursion with iteration, though.

def __str__(self):

    def strHelper(current):
        while current is not None:
            yield str(current.getData())
            current = current.getNext()

    return f'(head) {" ".join(strHelper(self._head))} (tail)'

Your string helper also forms the basis for a more general iterator:

def __iter__(self):
    current = self._head
    while current is not None:
        yield current
        current = current.getNext()

def __str__(self):
    return f'(head) {" ".join(str(x.getData()) for x in self)} (tail)'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.