如何掷骰子并计数骰子值出现的频率并返回计数

时间:2018-10-18 01:43:35

标签: python

这是我编写的代码:

import random

def main():
    print(get_dice_throws_result(30000, 6), "sixes thrown (out of 30000 throws)")    
    print(get_dice_throws_result(6, 6), "sixes thrown (out of 6 throws)")
    print(get_dice_throws_result(600000, 6), "sixes thrown (out of 600000 throws)")

    def get_dice_throws_result(num_dice_throws, num_to_check):
        count = 0
        current_throws = 0
        while current_throws < num_dice_throws:
            dice_num = random.randrange(1,7)
            if dice_num == num_to_check:
               count = count + 1
            current_throws += 1

        return count

main()

对于如何修改我的代码我很困惑,这是我的输出: 6投0次 600000投掷,99929个击掌

0 个答案:

没有答案