Basic Python Question, Working with Function and List [closed]

I'm new to programming and learning Python over the web and I'm stuck on this problem.

The objective is to have a list of numbers and create a function that will evaluate that list of numbers and print out the matches and the position in the list the matches are found at.

I think I'm 90% there, but the function I wrote only prints out the first match it finds, then ends.

Here's what I have so far:

    list=[36, 36, 79, 96, 36, 91, 77, 33, 19, 3, 34, 70, 12, 12, 54, 98, 86, 11, 17, 17]

def find(list,x):
for i in range(len(list)):
if x == i:
print ("Found",x,"at position",i)

find(list,12)
find(list,91)
find(list,80)


This correctly prints out:

"Found 12 at position 12", but fails to print the complete answer which is:

Found 12 at position 12 Found 12 at position 13 Found 91 at position 5

It appears the loop is ending prematurely, but I don't understand why. I realize this is probably pretty basic stuff, but I appreciate your time and any advice. Thanks in advance.

• This would be better suited to a general programming forum as it does not really relate to game development. May 18 '13 at 2:00
• This would be better suited on stackoverflow.com, as it is not really related to game development. May 18 '13 at 2:42

The problem is that you are checking the index of the element, not the actual element itself. Substitute list[i] for i and it works, for:

list=[36, 36, 79, 96, 36, 91, 77, 33, 19, 3, 34, 70, 12, 12, 54, 98, 86, 11, 17, 17]

def find(list,x):
for i in range(len(list)):
if x == list[i]:
print ("Found",x,"at position",i)

find(list,12)
find(list,91)
find(list,80)


However, this isn't very idiomatic ("pythonic") code. Using enumerate to iterate over a pairing of index/value, we can rewrite it as:

lst = [36, 36, 79, 96, 36, 91, 77, 33, 19, 3, 34, 70, 12, 12, 54, 98, 86, 11, 17, 17]

def find(lst, x):
for i,value in enumerate(lst):
if value == x:
print("Found {0} at position {1}".format(value, i))

find(lst,12)
find(lst,91)
find(lst,80)

• Thanks! I didn't know about enumerate. Just starting this stuff. Bookmarked that link! May 18 '13 at 15:49

This belongs on stackoverflow.com.

This being said, you check if the current index (i) is equal to the desired number (x), so it will always return the same number as x.

Instead, you want to see if the item in the list at the current index is equal to x, like so: if list[i] == x:.

The index represent the current position that you're at, say first item, second, etc. You are looking for the item equal to x, so this is the nuance.

Good luck with the rest, and feel free to ask game related questions here, but make sure they are specifically about games! :)

• Much appreciated. The "class" I'm taking is about building games in Python, but it makes much more sense to post a question like this in the general forum. Will do going forward. Appreciate the advice! May 18 '13 at 15:51
• @user30869 Feel free to come here for anything else game-related though! We love to help, although we just want the questions to go to the according site. :) Stackoverflow is a fantastic resource with incredibly fast feedback for anything about general programming. May 18 '13 at 16:39