Struggles [solved]

Sir, how can we do it with for loop? Please suggest .

1 Like

What are you asking? Did you skip the lesson on loops and go straight to the challenge?

1 Like

Sir, can you please explain me this code. link ==https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/lessons/python-functions-loops-cc/exercises/remove-even-nums

1 Like

Please post what code you have so far.

1 Like

def delete_starting_evens(lst):
while (len(lst) > 0 and lst[0] % 2 == 0):
lst = lst[1:]
return lst

#Uncomment the lines below when your function is done
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))

2 Likes

Is that your code or the given solution? What is it that you do not understand?

1 Like

I don’t understand how this given is working for the question given here.

1 Like

It’s checking to see that the list is not empty, first, then checking the first element to see if it is even. If so, line inside the while block assigns all but the first element (a slice) back onto the lst variable. The loop terminates if it encounters an odd number, or if the list is empty.

5 Likes