Hal Lawrence Hal Lawrence - 1 year ago 81
Python Question

Function to find position of variable?

Learning python and trying to return the row of where the string "A" is in the list, but its returning none in the visualizer. Its supposed to return the row index 1. Any idea what I'm doing incorrectly?
Code I have so far:

locale3 = [['.', '.', '.', 'e'], ['A', 'A', '.', 'e'], ['.', '.', '.', 'e'], ['.', 'X', 'X', 'X'], ['.', '.', '.', '.'], ['.', 'y', 'Z', 'Z']]
def row_locale(locale):
for i in range(len(locale)):
if locale[i] == "A":
return len(locale)

Answer Source

Each element of locale is an array of strings. When you do

if locale[i] == "A":

You compare such array to a single string "A", which is always false. If you want to find a locale that has "A", change it to this:

if "A" in locale[i]:

EDIT: You also return the length of the matching locale. You probably want to return i instead?