python中用于线性搜索的代码不起作用并输出错误的文本
我需要写一个程序,对句子中的一个字符进行线性搜索。我必须在不使用任何内置函数的情况下执行此操作,除了python中用于线性搜索的代码不起作用并输出错误的文本,python,linear-search,Python,Linear Search,我需要写一个程序,对句子中的一个字符进行线性搜索。我必须在不使用任何内置函数的情况下执行此操作,除了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 chara
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)
def线性搜索(intList,target):
发现=错误
计数=0
当计数小于len时(intList):
如果intList[count]==目标:
打印(“字符是”,计数,“在索引上”)
找到=真
打破
如果输入列表[计数]!=目标:
打印(“字符为-1”)
计数=计数+1
返回计数
句子=输入('输入句子:')
character=input('输入字符:')
character\u found=线性搜索(句子、字符)
非常感谢你的帮助 问题在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)
这些线路是:
if intList[count] != target:
print("The character is -1.")
count = count + 1
在这里,如果角色与目标不同,它将立即打印出“角色为-1”。但是,您希望在查看字符串中的每个元素后执行此操作,而不是在它只命中一个不相同的字符时执行此操作
您希望它在最后打印,因此,您的线性搜索函数应该更像这样:
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
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)
def线性搜索(intList,target):
发现=错误
计数=0
当计数小于len时(intList):
如果intList[count]==目标:
打印(“字符是”,计数,“在索引上”)
找到=真
打破
其他:计数+=1
如果未找到:
打印(“字符为-1”)
返回计数
句子=输入('输入句子:')
character=input('输入字符:')
character\u found=线性搜索(句子、字符)
如果任何答案都能解决您的问题,那么最好接受它们。接受也会给你一个小的提升