简体   繁体   English

python程序没有给出期望的结果

[英]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. 输入无法找到的搜索词时,所需的所有内容都接受吗?它不会打印“未找到”,而只会继续询问输入。

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
  1. Change if scores[index] == Searchword : to if names[index] == Searchword : if scores[index] == Searchword :更改为if names[index] == Searchword :
  2. Place Searchword= input('enter a searchword:') outside the while loop Searchword= input('enter a searchword:')放在while循环之外

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM