mardi 27 juin 2017

Why does this total not amount to 1000?

When I run the program below, the total is not 1000. I have no idea what is wrong.

In 1000 tosses of a dice, there were:

  • 180 for 1
  • 136 for 2
  • 121 for 3
  • 97 for 4
  • 72 for 5
  • 60 for 6.

This totals to 666 rolls of the dice.

I am trying to be specific, if there is anything I am not clear about, please let me know. And thanks everyone:)

#this is a program that simulate how many times that there will be for every sides of a dice, when I trying to throw it 1,000 times.

from random import randrange

def toss():
    if randrange(6) == 0:
        return "1"
    elif randrange(6) ==1:
        return "2"
    elif randrange(6) ==2:
        return "3"
    elif randrange(6) ==3:
        return "4"
    elif randrange(6) ==4:
        return "5"
    elif randrange(6) ==5:
        return "6"

def roll_dice(n):
    count1 = 0
    count2 = 0
    count3 = 0
    count4 = 0
    count5 = 0
    count6 = 0
    for i in range(n):
        dice = toss()
        if dice == "1":
            count1 = count1 + 1
        if dice == "2":
            count2 = count2 + 1
        if dice == "3":
            count3 = count3 + 1
        if dice =="4":
            count4 = count4 + 1
        if dice == "5":
            count5 = count5 + 1
        if dice == "6":
            count6 = count6 + 1
    print ("In", n, "tosses of a dice, there were", count1, "for 1 and", 

count2, "for 2 and", count3, "for 3 and", count4, "for 4 and", count5, "for
 5 and",count6, "for 6.")

roll_dice(1000)




Aucun commentaire:

Enregistrer un commentaire