mardi 30 avril 2019

How do I generate a random number, put them into dictionary and then give back the count of repetitions

I have the task to create a function that is calculating a random number, which is then added into a dictionary until a this number is randomly generated again. As a result the number of repetitions has to be printed into the console.

I have already tried the following but I do not get results. I am a Python beginner so I also feel a bit lost.

import random

def repetition(a, b):
d = dict()
x = random.randint(a ,b)
for i in d:
    if x in d.values:
        return len(dict)
    else:        
        dict.update(x)

In the last step the results should be formated as a tuple of the generated numbers and then last but not least the number of repetitions which might look like this:

repetition(2, 5255)
([1244, 2415, ... ], 122)




Aucun commentaire:

Enregistrer un commentaire