0

In my first week of learning cs and working on my first project assignment (in python3) I have hit a roadblock. There is an inbuilt function in the project which I'm unable to access The function basically does this-

"A test dice is deterministic: it always cycles through a fixed sequence of values that are passed as arguments. Test dice are generated by the make_test_dice function."

test_dice = make_test_dice(4, 1, 2)
>>>test_dice()
4
1 on the 2nd call, 2 on the 3rd call and 4 on the 4th call 

Now after researching for quite a while I came across the args and yield function for doing this job but I've been unable to implement it due to errors.

Any help on implementing this defined function would be appreciated!

David Buck
  • 3,752
  • 35
  • 31
  • 35
11junkyard
  • 35
  • 4
  • So what have you tried and what are the errors? In general you're more likely to get help if you show people what you have attempted and the errors you've received than if you just hint at having tried something and saying that it doesn't work. – David Buck Dec 09 '21 at 07:06

1 Answers1

0

You could do the following with cycle from the standard library itertools:

from itertools import cycle

def make_test_dice(*args):
    next_dice = cycle(args)
    def dice():
        return next(next_dice)
    return dice

With

test_dice = make_test_dice(4, 1, 2)
test = [test_dice() for _ in range(5)]

you'll get

[4, 1, 2, 4, 1]

But: Is this really your first Python assignment?

Timus
  • 10,974
  • 5
  • 14
  • 28
  • 1
    Thanks a ton for this! I had solved a few basic questions in the past week, but this is a part of the first serious assignment I've undertaken. It's a dice game of sorts. – 11junkyard Dec 09 '21 at 15:31