dimanche 17 janvier 2021

Python - High order function - make_fair_dice

I am working on a project. I need to create dice. And I have a solution below:

from random import randint

def make_fair_dice(sides):
    """Return a die that returns 1 to SIDES with equal chance."""
    assert type(sides) == int and sides >= 1, 'Illegal value for sides'
    def dice():
        return randint(1,sides)
    return dice

four_sided = make_fair_dice(4)
six_sided = make_fair_dice(6)

I don't understand why the solution created a inner function dice(). I think it's not necessary. But I knew I probably missed a important concept. As always code is not hard but concept.

Aucun commentaire:

Enregistrer un commentaire