user3000842
user3000842

Reputation: 45

python program not giving desired result

def main():
    names=[0]*10
    for index in range(len(names)):
        names[index] = input("Enter word " + str(index + 1) + ": ")
    bubbleSort(names)
    print("Names in Alphabetical order:")
    print(names)
def bubbleSort(names):
    for maxElement in range(len(names)-1, 0, -1):
        for index in range(maxElement):
            if names[index] > names[index+1]:
                temp = names[index]
                names[index] = names[index+1]
                names[index+1] = temp
    found = False
    index=0
    while found == False and index < len(names):
       Searchword= input('enter a searchword:')
       if scores[index] == Searchword :
            found = True
        else:
            index = index + 1
    if found:
        print("Found")
    else:
        print("Not Found")

main()

Does everything required accept when a Searchword is entered that cannot be found it does not print 'not found' but only keeps asking for input.

Upvotes: 0

Views: 74

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114528

  1. Change if scores[index] == Searchword : to if names[index] == Searchword :
  2. Place Searchword= input('enter a searchword:') outside the while loop

It should look something like this:

def main():
    names=[0]*10
    for index in range(len(names)):
        names[index] = input("Enter word " + str(index + 1) + ": ")
    bubbleSort(names)
    print("Names in Alphabetical order:")
    print(names)
def bubbleSort(names):
    for maxElement in range(len(names)-1, 0, -1):
        for index in range(maxElement):
            if names[index] > names[index+1]:
                temp = names[index]
                names[index] = names[index+1]
                names[index+1] = temp
    found = False
    index=0
    Searchword= input('enter a searchword:')
    while found == False and index < len(names):
       if names[index] == Searchword :
            found = True
        else:
            index = index + 1
    if found:
        print("Found")
    else:
        print("Not Found")

main()

Upvotes: 3

suhailvs
suhailvs

Reputation: 21740

you might need input before the loop, ie:

Searchword= input('enter a searchword:')    
while found == False and index < len(names):       
   if scores[index] == Searchword :
        found = True
    else:
        index = index + 1

Upvotes: 3

Related Questions