-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0021_searching.py
More file actions
35 lines (21 loc) · 799 Bytes
/
0021_searching.py
File metadata and controls
35 lines (21 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
shopping_list = ['milk', 'pasta', 'eggs', 'spam', 'bread', 'rice']
item_to_find = 'spam'
found_at = None # like null, does have a value
for index in range(len(shopping_list)): # len is the size of the array
if shopping_list[index] == item_to_find:
found_at = index
break
print('Item found at position {}'.format(found_at))
print('----')
item_to_find = 'albatross'
found_at = None
# for index in range(len(shopping_list)): # len is the size of the array
# if shopping_list[index] == item_to_find:
# found_at = index
# break
if item_to_find in shopping_list:
found_at = shopping_list.index(item_to_find)
if found_at is not None:
print('Item found at position {}'.format(found_at))
else:
print('{} not found'.format(item_to_find))