0

This is a part of our assignments for Computer Programming in high school. These are the instructions. https://prospect.schoolloop.com/file/1407107943880/1376109333800/2888156401519472863.pdf

def c7():
    myList = [7, 10, 94, 2, 54, 2, 19, 4, 11]
    print (myList)
    valueToSearch = int(input("Enter the value to search for: "))

    for n in range(0, len(myList)):
        if valueToSearch == myList[n]:
            print (myList[n], "is in index", n)
        elif (n == len(myList)-1) and (valueToSearch != myList[n]):
            print ("No matches")

For example:

>>> c7()
[7, 10, 94, 2, 54, 2, 19, 4, 11]
Enter the value to search for: 1
No matches
>>> c7()
[7, 10, 94, 2, 54, 2, 19, 4, 11]
Enter the value to search for: 7
(7, 'is in index', 0)
No matches

The last line "No matches" is unexpected when the input is 7.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
CateDoge
  • 55
  • 1
  • 8
  • Because you **always** check on each iteration. Regardless of whether or not you've found your value... so regardless of whether you find `19` at index `6`, you still check `elif (n == len(myList)-1) and (valueToSearch != myList[n])`, which happens to be true on the last iteration... – juanpa.arrivillaga May 03 '18 at 05:22

4 Answers4

1

Try this:

def c7():
    myList = [7, 10, 94, 2, 54, 2, 19, 4, 11]
    print (myList)
    valueToSearch = int(input("Enter the value to search for: "))

    for n in range(0, len(myList)):
        if valueToSearch == myList[n]:
            print (myList[n], "is in index", n)
        elif (n == len(myList)-1) and (valueToSearch not in myList):
            print ("%s is not found in list"%valueToSearch)
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
1

If you find the value in the list, you should stop searching.

for n in range(0, len(myList)):
    if valueToSearch == myList[n]:
        print (myList[n], "is in index", n)
        break
    elif (n == len(myList)-1) and (valueToSearch != myList[n]):
        print ("No matches")

You could then improve this by using python's for-else clause:

for n in range(0, len(myList)):
    if valueToSearch == myList[n]:
        print (myList[n], "is in index", n)
        break
else:
    print ("No matches")
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

Think about what happens if you enter a number that isn't 11. Let's say you pick 2.

The final item is reached. At this point, n == len(myList)-1) is True, and since 2 != myList[n], that condition is True as well, so "No Matches" is printed.

You could use another variable (e. g. found = False) that is set to True as soon as the first match is found, and then check for that at the end of the loop.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

You iterate over the list, for each value it checks if it is equal to your looked for value, if it is, it prints the one thing, else the other - hence you get one output per element in the list.

Edit: reread and saw you need to find all, not only the first:

def c7():
    myList = [7, 10, 94, 2, 54, 2, 19, 4, 11]
    print (myList)
    valueToSearch = int(input("Enter the value to search for: "))

    foundOne = False # none found so far
    for idx,value in enumerate(myList): # enumerate gives you index + value of each elem
        if value == valueToSearch:
            print (valueToSearch, "is in index", idx)    
            foundOne = True  # found (at least) one

    if not foundOne: 
        print ("No matches")

If you had to only get the first, simply get the index of the element in the list and capture the error if it is not in it.

That fits with pythons ask forgiveness not permission:

def c7findFirst():
    myList = [7, 10, 94, 2, 54, 2, 19, 4, 11]
    print (myList)
    valueToSearch = int(input("Enter the value to search for: "))
    try:
        idx = myList.index(valueToSearch)
        print (valueToSearch, "is in index", idx)
    except ValueError: # capture the error if thing is not in list
        print ("No matches")
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69