擅长:python、mysql、java
<p>请尝试以下<code>commacode</code>函数:</p>
<pre><code>monty = ['apples', 'bananas', 'tofu', 'cats', 'dogs', 'pigs']
def commacode(listname):
listname[len(listname) - 1] = 'and ' + listname[len(listname) - 1]
index = 0
new_string = listname[index]
while index < len(listname) - 1:
new_string = new_string + ', ' + listname[index + 1]
index = index + 1
if index == len(listname) - 1:
print(new_string)
commacode(monty)
</code></pre>