lundi 7 mars 2022

Python: Adding random integers to list if criteria met

In Python I am trying to make function make(ls) generate a list of 4 random integeres. If these integers passes the three checks in the function check(ls) they should be added as a list to res. If they do not pass another set will be generated until I have a list og 4 lists. The code below gives me IndexError: list index out of range - Any idea?

def check(ls):
    for n in ls:
        if n==0 or ls[0] == ls[2] or ls[1] == ls[3]:
            return False
    return True 

def make(ls):
    res = []
    while len(res) < 4:
        tmp = [randint(1,9),randint(1,9),randint(1,9),randint(1,9)]
        if check([tmp]) == True:
            res.append(tmp)
    return res



Aucun commentaire:

Enregistrer un commentaire