0
from random import randrange

def roll2dice() -> int:
    roll2 = []
    for i in range(50):
        sum = randrange(1,7) + randrange(1,7)
        roll2.append(sum)
    return roll2

The above function is for generating the random rolling sum of two die.

def distribution (n: int):
    result = []
    for x in range(2,13):
        result.append(roll2dice())
    for x in range(2,13):
        dice = result.count(x)
        num_of_appearances = result.count(x)
        percentage = (result.count(x) / int(n)) * 100
        bar = result.count(x)*'*'
    print("{0:2}:{1:8}({2:4.1f}%)  {3.5}".format(dice, num_of_appearances, percentage, bar))

I then used roll2dice to create a distribution function in which

distribution(200)

should yield:

 2:     7 ( 3.5%)  *******
 3:    14 ( 7.0%)  **************
 4:    15 ( 7.5%)  ***************
 5:    19 ( 9.5%)  *******************
 6:    24 (12.0%)  ************************
 7:    35 (17.5%)  ***********************************
 8:    24 (12.0%)  ************************
 9:    28 (14.0%)  ****************************
10:    18 ( 9.0%)  ******************
11:     9 ( 4.5%)  *********
12:     7 ( 3.5%)  *******

However, the error says:

Traceback (most recent call last):
  File "C:/Python34/lab6.py", line 23, in <module>
distribution_of_rolls(200)
  File "C:/Python34/lab6.py", line 21, in distribution_of_rolls
    print("{:2d}:{1:8d}({2:4.1f}%)  {3.5s}".format(dice_num, num_of_appearance, percentage, stars))
ValueError: cannot switch from automatic field numbering to manual field specification
sfjac
  • 7,119
  • 5
  • 45
  • 69
Ramon Hallan
  • 125
  • 12

1 Answers1

1

The first 3 fields are {n_field:format} The last field is {3.5}, without field number.

Here is a working version of (what I think) you want to do:

from random import randrange

def distribution (n: int):
    result = []
    for x in range(n):
        sum = randrange(1,7) + randrange(1,7)
        result.append(sum)
    for dice in range(2,13):
        num_of_appearances = result.count(dice)
        percentage = (num_of_appearances / n) * 100
        bar = int(percentage) * '*'
        print("{0:2}:{1:8} ({2:4.1f}%)  {3}".format(dice, num_of_appearances, percentage, bar))

Or, with a list comprehension:

from random import randrange

def distribution (n: int):
    for dice in [randrange(1,7) + randrange(1,7) for _ in range(n)]:
        num_of_appearances = result.count(dice)
        percentage = (num_of_appearances / n) * 100
        bar = int(percentage) * '*'
        print("{0:2}:{1:8} ({2:4.1f}%)  {3}".format(dice, num_of_appearances, percentage, bar))
stenci
  • 8,290
  • 14
  • 64
  • 104
  • Since I'm supposed to use the roll2dice function in the distribution function, where should I incorporate it? I've tried to do "for x in range(n): result.append(roll2dice()), but that didn't work. – Ramon Hallan Oct 31 '15 at 19:20
  • `append` will add a list to the list, and you would end up with a list of lists. You could use `extend`, which will add the elements of the list to the list. It kind of defeats the purpose of having a function, that's why I removed it. Perhaps I don't understand why you are using it. – stenci Oct 31 '15 at 19:42