Can someone please explain to me what this code is doing? More specifically so,
ID: 3703677 • Letter: C
Question
Can someone please explain to me what this code is doing? More specifically so, if you can explain what the double indexing -----> if list1[counter][0:-1].isdigit(): is doing, and what ship1 = Shipment(list1[counter][0:-1]). I don't understand what [counter] [0:-1] achieves. This deals with objects and classes basically. Thanks!
#MAIN
def main(list1):
result = []
counter = 0
while counter < len(list1):
if list1[counter][0:-1].isdigit():
if counter > 2:
result.append(ship1)
ship1 = Shipment(list1[counter][0:-1])
counter += 1
else:
if list1[counter][-7] != " ":
raise ItemException(Exception)
name = list1[counter][0:-7]
id = list1[counter][-6:-1]
price = list1[counter + 1][0:-1]
if price[0] != "$" or price[-3] != "." or "-" in price or price.count('.') > 1:
raise ItemException(Exception)
item1 = Item(name, id, price)
ship1.addItem(item1)
counter += 2
result.append(ship1)
return result
Explanation / Answer
isdigit returns true if all characters in the string are digits We can assume that list1 is a list of strings since isdigit is used list1[i] is used to access ith string in the list list1[i][0:-1] is used to access (0 to len(ith string)-1)characters of the ith string. list1[i][0:-1].isdigit returns true if in the ith string characters from (0 to len(ith string)-1) are all digits