pythonpython-3.xlistnullnul

How do we check if a value in an array is Null (None) or not?


my code:

import random

print("AI THAT WILL FIND OUT YOUR 5 letter text")
ttt = input()

fin = False
lenof = 25
s = 0
used = []
lol = False
used =  []

five = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

while fin == False:
    for i in five:
        ran = random.randint(s,lenof)
        r = five[ran]
        ran1 = random.randint(s,lenof)
        r = r + five[ran1]
        ran2 = random.randint(s,lenof)
        r = r + five[ran2]
        ran3 = random.randint(s,lenof)
        r = r + five[ran3]
        ran4 = random.randint(s,lenof)
        r = r + five[ran4]
        e = r

        if r == ttt:
            fin = True
            lol = True
            print(">> " + r)
        elif r != ttt and lol == False and used.index(r) is None:
            print("> " + r)
            used.append(e)

the error:

  File "<string>", line 38, in <module>
ValueError: 'ntzhk' is not in list

So can you please tell me how to check if a value in an array is None or not?

do i need to change anything?

i added that so the program will not repeat the same random letters.

Thanks.


Solution

  • Instead of :

    used.index(r) is None
    

    you can directly check for existence via:

    r not in used
    

    That showed error as used.index(r) doesn't return None if not found.