Search code examples
python-3.xspace-efficiency

Is there a better way to repeat code other than using def


I have

    print(randint(0,12))

Is there a quicker easier way of being able to execute this three times in a line other than copying it all out or using

    def RandNum(number):
        print(randint(0,12))

so is there just a way I can run the first piece of code really quickly instead of then going on and using.

    (RandNum("number"))

When ever I need the random number?

Thanks. I want to find as many alternative methods to doing this as possible.


Solution

  • Hope this helps!!

    from random import randint
    
    def RandNum(number):
        l = []
        for _ in range(number):
            l.append(randint(0,12))
        print " ".join(map(str, l))
    
    RandNum(3)