0

I am stuck on a question which asks:

Write a function print_matching_indexes(items, target) that prints the indexes of all the occurrences of the target in the list items. We are using for loops for iterating over lists

I have written:

def print_matching_indexes(items, target):
    """Prints the indexes of all (target) in the list (items)"""
    for i in items:
        print (items.index)

But I am receiving the message:

<built-in method index of list object at 0x7fc8103caa00>
<built-in method index of list object at 0x7fc8103caa00>
<built-in method index of list object at 0x7fc8103caa00>

If it works the following test code should result in:

pets = ['dog', 'cat', 'fish', 'cat', 'dog', 'iguana']
print_matching_indexes(pets, 'cat')

Result

1
3

Any pointers of where I'm going wrong?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
coschal
  • 21
  • 3
  • 4
    "" Okay, see where it says "method" in there? Do you know what a "method" is? Do you know how to use them? Hint: What happens if you just write `print_matching_indexes` instead of `print_matching_indexes(pets, 'cat')`? Why does that happen? – Karl Knechtel Mar 23 '21 at 23:53
  • 1
    The problem doesn't have anything to do with the `for` loop, so I removed all the tags about looping. Also, the `basic` tag is for the BASIC programming language, not for questions that you think are "basic". But if you think your question is "basic" then you should first try following a tutorial, or checking a checklist of common errors (when something goes wrong, you should make a note of it, so you can avoid making the same mistake in the future). – Karl Knechtel Mar 23 '21 at 23:54
  • 1
    @Jan this shouldn't be closed as a duplicate. OP wants the indices rather than the matching elements, and anyway understanding the typo is more important than the overall problem the code is trying to solve. It should instead be closed as caused by a typo, because it is. – Karl Knechtel Mar 23 '21 at 23:56
  • 1
    Look up the [`index` method](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) from the Python docs. There's even an example code there on how common list methods are used. – Gino Mempin Mar 24 '21 at 00:00
  • @KarlKnechtel This is a for loop exercise? – coschal Mar 24 '21 at 00:00
  • Yes, it is a for loop exercise. You wrote the for loop correctly, as evidenced by the fact that you expect to see a separate output for each item of the list, and you do in fact see a separate output for each item of the list. The problem is with the code *in* the for loop, which *would be wrong whether your code had a loop or not*. That's why the tags are inappropriate. – Karl Knechtel Mar 24 '21 at 00:08
  • 1
    That said: the `index` method is designed to look at the entire list for you, but only find a single match. That's not what you want to do - first off because you want all the matches, and second because you have been given an exercise to use the loop. You should *think more about what should happen each time through the loop*. For example, think about what you expect `i` to be equal to. Can you think of a way to use that information to figure out whether a given element of the list is equal to the `target`? Can you think of a way to figure out what position of the list you're at? – Karl Knechtel Mar 24 '21 at 00:11
  • (Hint: the `index` method will **not** work for the last part, because it only finds the first match, and you might not be looking at the first match.) – Karl Knechtel Mar 24 '21 at 00:11
  • Anyway, again: *Stack Overflow is not a tutorial website*. Try to write out, in plain English words, exactly what you think each step of the solution should be; write out, in plain English words, what the purpose of the `for` loop is and how you expect it to help you solve the problem. I'm serious about this part: take out a real piece of paper and a real pen or pencil and physically write it. That will force you to really think about what you mean. And then, if you still can't figure it out, try asking on an actual forum website such as https://reddit.com/r/learnpython. – Karl Knechtel Mar 24 '21 at 00:13

0 Answers0