Print a list of items separated by commas but with the word "and" before the last item
<p>I've been working on this for two days. This is what the assignment states:</p>

<blockquote>
  <p>Say you have a list value like this: <code>listToPrint = ['apples', 'bananas', 'tofu', 'cats']</code> Write a program that prints a list with all the items separated by a comma and a space, with "and" inserted before the last item. For example, the above list would print <code>'apples, bananas, tofu, and cats'</code>. But your program should be able to work with any list not just the one shown above. Because of this, you will need to use a loop in case the list to print is shorter or longer than the above list.</p>
</blockquote>

<p>This is what I have thus far:</p>

<pre><code>listToPrint = []
while True:
    newWord = input("a, b, and c ")
    if newWord == "":
        break
    else:
        listToPrint.append(newWord)
</code></pre>

<p>Here is how I would do it, but be careful if this is for school.  Your instructor will frown on you if any of the things I have done below are using features or techniques that haven't yet been covered.</p>

<pre><code>listToPrint = ['a', 'b', 'c']

def list_to_string(L, sep = '', last_sep = None):
    if last _sep is None:
        return sep.join(L)
    else:
        return sep.join(L[:-1]) + last_sep + L[-1]

print(list_to_string(listToPrint, sep = ', ', last_sep = ', and '))
</code></pre>

<p>Here's a bit more of a beginner version: </p>

<pre><code>listToPrint = ['a', 'b', 'c']
list_length = len(listToPrint)

result = ""
count = 0
for item in listToPrint:
    count = count + 1
    if count == list_length:
        result = result + "and " + item
    else:
        result = result + item + ", "
</code></pre>

<p>This one doesn't work with only one item in the list.  </p>

python list python-3.x loops for-loop