Not iterable

def elementsearch(randomlist,x):
randomlist = randomlist.sort()

for number in randomlist:
    if x == number:
        print ('true')
        return True
    else:
        print ('false')
        return False

if name == “main”:
randomlista = [1,2,6,4,3,2,4,5,2]
elementsearch(randomlista,9)

the error i keep getting is TypeError: ‘NoneType’ object is not iterable
what does that mean and whats wrong with my code?
thanks!

lst.sort() sorts a list. That is all it does. It has no return value, which means that

x = lst.sort()
print(x)

… will print None , and for number in None: will return the error you are receiving.

Solution: don’t assign list.sort() to a variable. Just call lst.sort(), then proceed with lst.

I might add that your else statement should be aligned with your for statement, otherwise your function will only iterate through the first number in your list. Unless you’re were going to add something at the end of your function

thank you ! i have sorted that out . i tried with another code but it seemed like the console keeps showing ‘False’

randomlista = [1,2,6,4,3,2,4,5,2]

def elementsearch(randomlist):
randomlist.sort()
x = input(‘give a number’)
for number in randomlist:
if x == number:
print (‘true’)
return True
else:
print (‘false’)
return False

elementsearch(randomlista)

Any value assigned via input() will be a string. If you want a valid comparison with == to an int, you must convert the string to int.

Also your can use the </> icon to post your code

randomlista = [1,2,6,4,3,2,4,5,2]

def elementsearch(randomlist):
    randomlist = randomlist.sort()
    x = input("give a number:")
    x= int(x)
    for number in randomlist:
        if x == number:
            print ("true")


        else:
            print ("false")


elementsearch(randomlista)

i did that but it still shows
Traceback (most recent call last):
File “/Users/Yushan/PycharmProjects/PracticePython/venv/Prime.py”, line 16, in
elementsearch(randomlista)
File “/Users/Yushan/PycharmProjects/PracticePython/venv/Prime.py”, line 7, in elementsearch
for number in randomlist:
TypeError: ‘NoneType’ object is not iterable

Process finished with exit code 1

lst.sort() is a list method. It sorts a list in place, and it returns None
sorted() is a builtin Python funcction. It returns a sorted version of any iterable that can be indexed.

my_lst = [8,3,7,4,6,5]
my_lst.sort()
print(my_lst)  # output 1, below

my_lst = [8,3,7,4,6,5]
p = my_lst.sort()
print(p)  # output 2

my_lst = [8,3,7,4,6,5]
q = sorted(my_lst)
print(q)  # output 3

print(my_lst)   # output 4

s = "dcba"
q = sorted(s)
print(q)  # output 5

s = "dcba"
s.sort()
print(s)  #output 6

Output:

[3, 4, 5, 6, 7, 8]  # 1. original list after lst.sort()
None                # 2. returned value from lst.sort()
[3, 4, 5, 6, 7, 8]  # 3. returned value from sorted()
[8, 3, 7, 4, 6, 5]  # 4. original list after sorted()
['a', 'b', 'c', 'd']  # 5. sorted() works with a string ...
Traceback (most recent call last):
  File "C:\path\to\test.py", line 20, in <module>
    s.sort()
AttributeError: 'str' object has no attribute 'sort'  # 6.  ... but sort() does not