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()
当输入无法找到的Searchword时,所有必需的内容都会接受它不会打印“未找到”但只是一直要求输入。
答案 0 :(得分:3)
你可能需要在循环之前输入,即:
Searchword= input('enter a searchword:')
while found == False and index < len(names):
if scores[index] == Searchword :
found = True
else:
index = index + 1
答案 1 :(得分:3)
if scores[index] == Searchword
:更改为if names[index] == Searchword :
Searchword= input('enter a searchword:')
放在while
循环看起来应该是这样的:
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()