Startup Data scientist Blog

データ分析系のテック情報を発信します

whileを使ったpythonコード

pythonコード

import random

roll = 0
count = 0

while roll != 5:
    count = count + 1
    roll = random.randint(1, 5)
    print(roll)
   
print('It took {count} rolls to roll a 5!')

 

 

Output

2
2
2
5
It took 4 rolls to roll a 5!

 

random関数をインポートすることでランダムに1から5の値を示す。