one of the two versions of my code has been rejected in this exercise
Version 1 is ok. But, version 2 works the same, but it was rejected ( the feedback is: “Function insert did not return the expected value”). What is the reason ?
list_1 = ["A", "B", "C", "E"]
# Version 1
def insert(list_x, object_x, index_x):
return list_x[:index_x] + [object_x] + list_x[index_x:]
print(insert(list_1, "D", 3))
# Version 2
def insert(list_x, object_x, index_x):
list_x.insert(index_x, object_x)
return list_1
print(insert(list_1, "D", 3))
I have one additional question to this exercise:
How can I place the argument in the definition that the value ( in this example: the value of the index) can only be positive? is there any way to do so? Or the only way is to put it inside the definition for ex:
def insert(list_x, object_x, index_x):
if index_x < 0:
return False
Im thinking about something like that ( pseudocode):
def insert(list_x, object_x, index_x >= 0):