samedi 8 octobre 2022

How can I get the program to ask user how many tries do they want to generate from a random pool of elements, each with different probabilities?

I am trying to create a piece of code that asks the user to input a desired amount of tries, for example: -How many do you want to generate? -Generate 5 random animals from a pool [Dog, Cat, Hippo, Panda].

  • Dog, Dog, Hippo, Dog, Cat

each animal has a different chance in the probability of showing up when it's generated. (see code below)


while True:
    option = input("Execute y/n")
    if option == 'y':
        animals = ['Dog', 'Cat', 'Hippo', 'Panda']
        weights = [0.5, 0.35, 0.1, 0.05]
        result = np.random.choice(animals, p=weights)
        print(result)
    else:
        break 

the problem with my code is that, it is not creating several tries and if I put, for example:

print(result*5) it prints the same result 5 times and not different results.




Aucun commentaire:

Enregistrer un commentaire