當編寫這段代碼時,我發現自己做了很多重複的事情,並想知道是否有一個更簡單或更簡單的方法來做重複性的任務如此。獲取整數的出現次數在一個範圍內的隨機數字選擇N次
下面是相關代碼:
from random import randint
RandomNumber = Zeroes = Ones = Twos = Threes = Fours = Fives = Sixes = i = 0
while i < 1000000:
RandomNumber = (randint(0,6))
if RandomNumber == 0:
Zeroes = Zeroes + 1
if RandomNumber == 1:
Ones = Ones + 1
if RandomNumber == 2:
Twos = Twos + 1
if RandomNumber == 3:
Threes = Threes + 1
if RandomNumber == 4:
Fours = Fours + 1
if RandomNumber == 5:
Fives = Fives + 1
if RandomNumber == 6:
Sixes = Sixes + 1
i = i + 1
工作代碼屬於http://codereview.stackexchange。com/ –
爲什麼在創建可以容納它們的'list'時使用大量變量? – ForceBru
對不起,將在下次發佈到codereview – Kos