Posts: 2
Threads: 1
Joined: Oct 2019
Oct-20-2019, 06:10 PM
(This post was last modified: Oct-20-2019, 06:14 PM by ichabod801.)
Hello,
Let me start by saying that i have no idea what im doing :). Turned 30, got a kid decided to lern coding for some reason (relly enjoy it so far).
Trying to make a program that roll 3 dices and list the probability in %.
so far i got the following:
1 2 3 4 5 6 7 8 9 10 11 12 |
dice_1 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
dice_2 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
dice_3 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
def roll_dice():
avrage = 0
for x in dice_1:
avrage = x
for y in dice_2:
avrage = x + y
for z in dice_3:
avrage = x + y + z "
|
When i run the def i get a list of every possible toss that can be made with 3 t6 dices. How can i now go forward? what im looking for is:
Output: chance to roll:
3 -> 4%
4 -> 5%
5 -> 7%
(and so on).
BR
Profeteus
Posts: 4,220
Threads: 97
Joined: Sep 2016
What you are going to get at the end of this is nothing. Your function does not return a value. If you put return avrage it would return 18, not the list of values you want. This is because you keep assigning a new value to avrage, over writing what was there before. That's what assignment (=) does.
If you want a list of all the possible values, you need to start with an empty list and append to it:
1 2 3 4 5 6 7 8 9 |
die_sides = ( 1 , 2 , 3 , 4 , 5 , 6 )
def roll_dice():
values = []
for die_1 in die_sides:
for die_2 in die_sides:
for die_3 in die_sides:
values.append(die_1 + die_2 + die_3)
return values
|
Now, from values you could get the number of times a 3 was rolled with values.count(3) . You could divide that by the total number of possible rolls, len(values) . But if you are just interested in the # of times each value is rolled, you could count them instead of storing them:
1 2 3 4 5 6 7 8 9 |
die_sides = ( 1 , 2 , 3 , 4 , 5 , 6 )
def roll_dice():
counts = [ 0 ] * 19
for die_1 in die_sides:
for die_2 in die_sides:
for die_3 in die_sides:
counts[die_1 + die_2 + die_3] + = 1
return counts
|
Now you get the number of times 3 was rolled with counts[3] , and the total number of possible rolls is sum(counts) .
Posts: 4,220
Threads: 97
Joined: Sep 2016
Oh, you can also use itertools to simplify this quite a bit:
1 2 3 4 5 6 7 8 9 |
import itertools
die = ( 1 , 2 , 3 , 4 , 5 , 6 )
def roll():
counts = []
for roll in itertools.product(die, repeat = 3 ):
counts[ sum (roll)] + = 1
return counts
|
Posts: 2
Threads: 1
Joined: Oct 2019
(Oct-20-2019, 06:26 PM)ichabod801 Wrote: What you are going to get at the end of this is nothing. Your function does not return a value. If you put return avrage it would return 18, not the list of values you want. This is because you keep assigning a new value to avrage, over writing what was there before. That's what assignment (=) does.
If you want a list of all the possible values, you need to start with an empty list and append to it:
1 2 3 4 5 6 7 8 9 |
die_sides = ( 1 , 2 , 3 , 4 , 5 , 6 )
def roll_dice():
values = []
for die_1 in die_sides:
for die_2 in die_sides:
for die_3 in die_sides:
values.append(die_1 + die_2 + die_3)
return values
|
Now, from values you could get the number of times a 3 was rolled with values.count(3) . You could divide that by the total number of possible rolls, len(values) . But if you are just interested in the # of times each value is rolled, you could count them instead of storing them:
1 2 3 4 5 6 7 8 9 |
die_sides = ( 1 , 2 , 3 , 4 , 5 , 6 )
def roll_dice():
counts = [ 0 ] * 19
for die_1 in die_sides:
for die_2 in die_sides:
for die_3 in die_sides:
counts[die_1 + die_2 + die_3] + = 1
return counts
|
Now you get the number of times 3 was rolled with counts[3] , and the total number of possible rolls is sum(counts) .
damn, that worked awsome! for the counts = [0] part, how do i turn the awnser into % from this part?
BTW; you are awsome !
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Oct-20-2019, 06:47 PM)profeteus Wrote: damn, that worked awsome! for the counts = [0] part, how do i turn the awnser into % from this part?
Like I said, you divide each count by the total number of values. So counts[3] / sum(counts) is the % chance you will roll a three (1 in 216).
|