Generators

import random

def lottery():
    # returns 6 numbers between 1 and 40
    for i in range(6):
        yield random.randint(1, 40)

    # returns a 7th number between 1 and 15
    yield random.randint(1,15)
    # returns a 8th number between 100 and 120
    yield random.randint(100,120)

for random_number in lottery():
       print("And the next number is... %d!" %(random_number))
  
  ========= RESTART: C:/python/lottery20210222.py ========
And the next number is... 25!
And the next number is... 25!
And the next number is... 16!
And the next number is... 38!
And the next number is... 22!
And the next number is... 4!
And the next number is... 9!
And the next number is... 115!
>>> 
  

コメント

このブログの人気の投稿

シェルピンスキーの三角形

global 変数・ローカル変数