Reputation: 9
#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.
Upvotes: -1
Views: 81
Reputation: 64
Firstly, setting a variable's name to list
is bad practice, as doing this overwrites the default list()
function in python (in this instance), so let's change the name of the variable in your script from list
to var
to minimize any chances of future issues/misunderstandings.
from sys import exit
var = []
for i in range(0, len(var) - 1):
try:
print(var[i] + ',', end = ' ')
except IndexError:
exit()
print("and " + var[-1])
Note that you defined var
as an empty list before we review your for-loop.
Review len(var) - 1
in for i in range(0, len(var) - 1)
. As the script is executing, remember that var
is still an empty list once the interpreter begins reading the line containing the for-loop. The len()
function counts how many elements are in a string, list, tuple, etc. So, in your for-loop, the iterator i
begins at 0
, and the nested code following your for-loop is executed until i
is greater-than-or-equal-to -1
, seeing as len(var) - 1
is equivalent to 0 - 1
.
In other words, any code that is nested under the for-loop is executed a total of -1
times -- meaning that any code nested under the for-loop is completely ignored, and the interpreter jumps straight to the print
function at the end of your script, which is where the IndexError
occurs.
Review var[-1]
in print("and " + var[-1])
. A negative index of a list in Python refers to accessing elements in a list by counting from the last element to the first element of the list, rather than from the first element to the last. In other words, var[-1]
points to whatever element is stored at index 0
of the reversed list, or (put more simply) var[-1]
would point to the last element of the list var
.
You're attempting to access the last element of an empty list, which will always throw an IndexError. The print function at the end of your script is not part of the try block, being why you're still receiving an IndexError.
Cheers.
Upvotes: 0
Reputation: 184
What is the length of your list when the print statement is called? If the list is empty, ANY index into the list is out-of-bounds.
There is also a secondary issue with your print statements. The + operator can be used to concatenate strings, so something like
value = "5"
print("and " + value)
works just fine. However, you can't use + to concatenate a string with a number, so this
value = 5
print("and " + value) # this is an error
will result in an error. One correct way to do it would be with the comma operator, like so:
value = 5
print("and", value)
Upvotes: 2
Reputation: 1646
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.
list=[]
for i in range(0,len(list)-1):
print(list[i]+',',end=' ')
if len(list)>0:
print("and "+list[-1])
Upvotes: -2