“TypeError:'type'的对象没有len()”Python上的线性搜索

2024-05-12 20:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python有点陌生,今天我想做一个线性搜索算法,但是我遇到了len(list)的问题:

def linearSearch(list, targetValue):
    for i in range(0, len(list)):
        if list[i] == targetValue:
            return i #function stops

    return -1

# MAIN

myList = [3, 2, 8, 1, 10]

location = linearSearch(list, 3)
print(location)

我希望输出什么也不给出或-1,因为我仍在测试 但我一直收到这个错误信息。在


Tags: inforlenreturnifdefrangefunction