Stuck with Python loop challenges: Delete Starting Even Numbers

Lesson in question.

This is a bit out there as I haven’t even gotten to the end as I am stuck in my code, unable to proceed. I know what I want to do, but I don’t know how to do it. The function as follows:

def delete_starting_evens(lst):
for num in lst:
if num % 2 == 0:
return lst[#position of num in the list:]

I don’t even know if the function would work, however I am stuck in the part I have highlighted in bold. How do I introduce the index position of the corresponding num? if I use num it would introduce the value, right?

Thank you in advance for any help.

Welcome to the forums!

I can’t see the highlighted part you’re referring to. Also, it’s imperative that you use indentation in Python code (assuming that you have the code indented in the lesson[?]) b/c it tells the interpreter what statements belong to what block of code.

So, the parameter is lst and remember, it can be any length (hint)…so you want to account for that. What kind of loop can you use when you’re not sure how many times you’ll need to loop through something?
You want the function to 1.) check if there are any elements in the list and 2.) if the first element is odd. (you’re already using % but you need to tweak that to account for the two things your’e checking for in the lst that you’re passing through as an argument.) And then, if those two conditions are True you want to slice off the first element in that list. How would you accomplish that?

1 Like

Hi there, thank you for your help, I am using indents, I most likely copied it over wrongly so it doesn’t shows. I have been trying while loops, but currently I have an infinite loop problem, I think is because something is not iterating while I think it does, could you have a look? Here is the code:

if len(lst) > 0:
for num in lst:
while num % 2 == 0:
lst = [1:]
return lst

I am guessing that the problem is:

while num % 2 == 0:

so basically I think I am telling it to do it forever? But if it is under the for block, shouldn’t it repeat with a new num?

To format your code here you can use the </> button at the top.

Eek, no. We definitely don’t want any infinite loops.

You don’t need to use if here. Did you try to replace your use of if with while by any chance? (this isn’t a case where we use if/else.
Also, how can you check to see if the first element of a list is % 2 ==0? (ie: what is the index of the first element of a list? ) You’re close.
and, how can we check if two conditions are met? Grammatical hint, what conjunction can we use?

Apparently I need more coffee to reply without editing!

1 Like

Unfortunately while going through the forum I stumbled upon the answer, so I don’t know if I would have figured it out myself.

However, I do have a question about the answer presented in the solution:

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

if I am understanding the function correctly, in the second line, lst[0] will be iterated every time it loops, why is that?

(the line in question)

while (len(lst) > 0 and lst[0] % 2 == 0):

The best way to see how the function works is to pass through a different variety of lists of varying lengths that contain both even and odd numbers. The directions, " The function should remove elements from the front of lst until the front of the list is not even. The function should then return lst ."

You’re checking to see that the list has at least one element

AND if the first element is even. If both those conditions are True…the list will slice off that element and start at the second element, or, [1:] The function will then return elements in the list.

Try replacing the first element with an even and the an odd number and see what the results are…and what happens if all elements in the list are even numbers?

I can definitely see your point about being confused as the interchangeable usage of the terms “not even” and “odd” are, well…confusing.

1 Like

Thank you, took me a long while to understand. I feel like I am hitting a wall suddenly.

You’re welcome.

It’s okay…these things–these walls-- happen to everyone. Know when to take breaks when studying/writing code too. And, if you can explain a concept or how a piece of code works to a rubber duck, then you’ve learned something. :slight_smile: :duck: