#I will write a program to spurt out objects of a list
import sys
list=[]
for i in range(0,len(list)-1):
try:
print(list[i]+',',end=' ')
except IndexError:
sys.exit()
print("and "+list[-1])
I tried putting the whole for loop in thw try clause and i still get IndexError im stuck. I had to find a way to deal with empty lists, because the code works with other lists.
Solution:
I don't think the try / except is necessary but all you really need to do is check the length of your list before trying to print a final element.