I am trying to have the make_test(['Jon', 'Kate'], 4)
at the end below generate a dictionary where the name (Jon, Kate) is key and the each have a four lists (each with four integers), like {'Jon': [[1,2,3,4], [4,3,7,-8], [-4,9,-3,-1], [-2,-9,-3,-1]], 'Kate': [[2,-1,-2,4], [-1,9,-8,-7],[1,2,5,-4], [-5,-4,-1,2]]}
. The lists with the four integers are made by make_eq
(it generates n number of lists, in this case four). make_eq
uses check to see if the three criterias set here are met. The code below just gives me an empty dictionary. Any ideas?
from random import randint
res = []
students = []
tests = {}
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_eq(ls):
while len(res) < n:
tmp = [randint(1,9),randint(1,9),randint(1,9),randint(1,9)]
if check(tmp):
res.append(tmp)
return res
def make_test(students, n):
for student in students:
for test in res:
tests[student] = test
res.remove(test)
return tests
make_test(['Jon', 'Kate'], 4)
CodePudding user response:
# Importing
from random import randint
# Functions
def make_eq(n: int):
"""A function used to generate 'n' numbers of list containing each four random integers from 1 to 9"""
lists: list = []
for _ in range(n):
lists.append([randint(1, 9) for _ in range(4)])
return lists
def make_dictionnary(keys: list, n: int):
"""A function that generate a dicitonnary containing 'n' numbers of four-integers-long list for each keys in 'keys'"""
students = {}
for key in keys:
students[key] = make_eq(n)
return students
# Testings
print(make_dictionnary(["Student 1", "Student 2", "Student 3"], 5))
The "make_eq" function generates a list containing 'n' numbers of lists containing, again, each 4 random integers.
The "make_dictionnary" generates a dictionnary, that returns a list (see "make_eq" for more details) for each student :) !
CodePudding user response:
After copying and pasting your code, it indeed returned an empty dictionary, the reason is that when you call make_test
your res
list is empty, resulting in the inner for loop not running and nothing is being added to tests. I could help you with the code but I did not really understand what you are trying to do, perhaps you need to run make_eq
before running make_test
.
In that case, the make_eq
is not using the ls
input variable and n
is not defined...