FAQ: Code Challenge: Loops - Same Values

4 posts were split to a new topic: Remember to check only index to index

2 posts were split to a new topic: Could this code be improved?

8 posts were split to a new topic: My solution

A post was split to a new topic: How come my code can’t pass?

4 posts were split to a new topic: How could I use list comprehension to solve this?

6 posts were split to a new topic: How can I improve this?

I am so frustrated with these code challenges. This last set took me forever to understand, and half (or more) of the time I can’t solve them on my own without looking at the hints and the help. This is really discouraging, because I feel confident about the syntax and the concepts, but these problems at the end of the units make me feel like a moron. On nights like tonight, it make me feel like giving up…like I’m never going to be able to apply this stuff in real life. Can anyone help?

3 Likes

Don’t give up. Never give up. The hundred times you don’t try will all end in failure. Climb that hill. It’s not a race, but a destination. From there you climb the next hill.

7 Likes

Thank you…I really appreciate your reply. It helps just to know that someone has my back! :slight_smile: This is one of most challenging things I’ve ever tried to learn (I have NO programming experience), and I had a bad night tonight. Back on the horse tomorrow! Thanks again.

2 Likes

A post was merged into an existing topic: How could I use list comprehension to solve this?

A post was split to a new topic: Convert psuedo code to actual program

I have a question about a hint we are given (I already solved it but in a different way). Why would we use range(len) if we know that both of those lists are the same length. Even if they are not, if you use for loop it will just go through every index of a list an then stop so why even bother to complicate with range?

using a for loop we get the values of the list:

for element in lst:

while using range gives use the indexes:

for index in range(len(list))):

so your question conflicts with what you are saying later on?

1 Like

I don’t get what you’re saying, conflicts how?

this conflicts:

with:

the for loop doesn’t gives us the indexes of the list, it gives us the values. That is why we use range(), this gives us the indices/indexes

1 Like

Don’t give up! This stuff is tough!!!

This one took me a second. I was doing a nested for loop at the beginning which was very incorrect haha

2 Likes

Thank you! I’m still chugging away, thanks to the help and support I’ve gotten here in the forums! :slight_smile:

Hi, I wanted to solve the code challenge that looks for duplicats in two list and returns the index position of the matching duplicates with a while loop. However, I got stuck with the code below.

Can anyone advice how to modify that while loop?
Thx

def same_values(lst1, lst2):
  new_lst = []
  index = 0
  while index <= (len(lst1)-1):
    if lst1[index] == lst2[index]:
      new_lst.append(index)
      index += 1
  return new_lst

you have an infinity loop. What if the first value of list one and two aren’t equal? Then index is never increased.