-1

it keeps giving me this error can you tell me why. I can't find the answer

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        x = len(List)
        for i in range(x):
            if List[i] == target:
                return i
            elif List[i] > target:
                return List[i-1]

TypeError: object of type '_SpecialGenericAlias' has no len()

quamrana
  • 37,849
  • 12
  • 53
  • 71
Rayan
  • 1
  • 1
  • 3

2 Answers2

2

len(List) is trying to check the len of the type alias List (which is used only for type hinting).

Even len(list) would have been wrong as it was trying to check the len of the list type.

The check should be len(nums) as nums is the name of variable that holds the actual list.

Similarly, List[i] and List[i-1] should be nums[i] and nums[i - 1].

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

You're not checking the length of nums, you're trying to check the lenght of List which is just an alias.

Replace x = len(List) with x = len(nums)

Ismail Hafeez
  • 730
  • 3
  • 10