#------------------------------------------------------------------------------ # LinearSearch1.py #------------------------------------------------------------------------------ def LinSearch(x, L): """ returns the first (i.e. leftmost) index i in L for which L[i]==x """ for i in range(len(L)): if x == L[i]: return i # end if # end for # if this line is reached, None is returned # end LinSearch() # function main() ------------------------------------------------------------- def main(): target = 'eight' words = ['one','two','three','four','five','six','seven','eight','nine','ten'] position = LinSearch(target, words) print(target, 'found at position', position) target = 50 numbers = [3, -2, 50, 78, 5, 50] position = LinSearch(target, numbers) print(target, 'found at position', position) target = 12 position = LinSearch(target, numbers) print(target, 'found at position', position) # end main() # closing conditional --------------------------------------------------------- if __name__=='__main__': main() # end