Advertisement
jabela

Linear Search

Feb 22nd, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. Array=['john','jack','jill']
  2.  
  3. # Flag to check if search found the value. Set to False by default
  4. checked = False
  5.  
  6. #function to check through the array
  7. def array_check(Userinput):
  8.     position=0
  9.     global checked
  10.     #global is needed to use a variable outside of the function
  11.     while position<len(Array):
  12.         if Userinput == Array[position]:
  13.             print('Found it')
  14.             checked = True
  15.  
  16.         position=position+1
  17.  
  18. # This is the second While loop. Using a function makes this much more elegant
  19. while checked == False :
  20.     Userinput = input('Type the item for search:')
  21.     array_check(Userinput)
  22.     print(checked)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement