I'm learning Python through Automate the Boring Stuff and I'm running into a something I don't quite understand.
I'm trying to create a simple for loop that prints the elements of a list in this format: W, X, Y, and Z
.
My code looks like the following:
spam = ['apples', 'bananas', 'tofu', 'cats']
def printSpam(item):
for i in item:
if i < len(item)-1:
print (','.join(str(item[i])))
else:
print ("and ".join(str(item[len(item)-1])))
return
printSpam(spam)
I get this error in response:
Traceback (most recent call last):
File "CH4_ListFunction.py", line 11, in <module>
printSpam(spam)
File "CH4_ListFunction.py", line 5, in printSpam
if i < len(item)-1:
TypeError: '<' not supported between instances of 'str' and 'int'
Any help is appreciated. Thanks for helping a newbie.