python 中的线性搜索代码不起作用并输出错误的文本
Code for linear search in python not working and outputs wrong text
我需要编写一个程序来对句子中的字符进行线性搜索。除了 print()
.
之外,我必须在不使用任何内置函数的情况下执行此操作
程序应该输出字符所在的索引。
如果该字符不在句子中,则应输出 -1 作为索引。
我的代码输出如下:
Enter a sentence: Hello
Enter a character: e
The character is -1.
The character is 1 on the index.
即使它应该只输出:
Enter a sentence: Hello
Enter a character: e
The character is 1 on the index.
下面是我的代码:
def linear_search(intList,target):
found = False
count = 0
while count < len(intList):
if intList[count] == target:
print("The character is", count, "on the index.")
found = True
break
if intList[count] != target:
print("The character is -1.")
count = count + 1
return count
sentence = input('Enter a sentence: ')
character = input('Enter a character: ')
character_found = linear_search(sentence,character)
非常感谢您的帮助!
问题出在 while 循环中。
行是:
if intList[count] != target:
print("The character is -1.")
count = count + 1
这里,如果字符与目标不一样,会立即打印出来"The character is -1"。但是,您想在查看字符串中的每个元素之后执行此操作,而不是在它只遇到一个不相同的字符时执行此操作。
你想要的是它在最后打印,所以你的 linear_search 函数应该看起来更像这样:
def linear_search(intList,target):
found = False
count = 0
while count < len(intList):
if intList[count] == target:
print("The character is", count, "on the index.")
found = True
return count
count = count + 1
print("The character is -1")
return -1
另一种使用更少代码且没有内置函数的方法是使用 for 循环,如下所示:
def linear_search(intList, target):
for char in intList:
if char == target:
print("The character is", count, "on the index.")
return count
count += 1
print("The character is -1")
return -1
您的问题是您在检查整个字符串之前输出了不需要的结果。您可以通过简单地检查是否在 while 循环结束后找到字符来轻松解决这个问题。
def linear_search(intList,target):
found = False
count = 0
while count < len(intList):
if intList[count] == target:
print("The character is", count, "on the index.")
found = True
break
else: count += 1
if not found:
print("The character is -1.")
return count
sentence = input('Enter a sentence: ')
character = input('Enter a character: ')
character_found = linear_search(sentence,character)
我需要编写一个程序来对句子中的字符进行线性搜索。除了 print()
.
程序应该输出字符所在的索引。
如果该字符不在句子中,则应输出 -1 作为索引。
我的代码输出如下:
Enter a sentence: Hello
Enter a character: e
The character is -1.
The character is 1 on the index.
即使它应该只输出:
Enter a sentence: Hello
Enter a character: e
The character is 1 on the index.
下面是我的代码:
def linear_search(intList,target):
found = False
count = 0
while count < len(intList):
if intList[count] == target:
print("The character is", count, "on the index.")
found = True
break
if intList[count] != target:
print("The character is -1.")
count = count + 1
return count
sentence = input('Enter a sentence: ')
character = input('Enter a character: ')
character_found = linear_search(sentence,character)
非常感谢您的帮助!
问题出在 while 循环中。
行是:
if intList[count] != target:
print("The character is -1.")
count = count + 1
这里,如果字符与目标不一样,会立即打印出来"The character is -1"。但是,您想在查看字符串中的每个元素之后执行此操作,而不是在它只遇到一个不相同的字符时执行此操作。
你想要的是它在最后打印,所以你的 linear_search 函数应该看起来更像这样:
def linear_search(intList,target):
found = False
count = 0
while count < len(intList):
if intList[count] == target:
print("The character is", count, "on the index.")
found = True
return count
count = count + 1
print("The character is -1")
return -1
另一种使用更少代码且没有内置函数的方法是使用 for 循环,如下所示:
def linear_search(intList, target):
for char in intList:
if char == target:
print("The character is", count, "on the index.")
return count
count += 1
print("The character is -1")
return -1
您的问题是您在检查整个字符串之前输出了不需要的结果。您可以通过简单地检查是否在 while 循环结束后找到字符来轻松解决这个问题。
def linear_search(intList,target):
found = False
count = 0
while count < len(intList):
if intList[count] == target:
print("The character is", count, "on the index.")
found = True
break
else: count += 1
if not found:
print("The character is -1.")
return count
sentence = input('Enter a sentence: ')
character = input('Enter a character: ')
character_found = linear_search(sentence,character)