Python Forum
Problems with a def function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with a def function
#1
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:

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
Reply
#2
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:

die_sides = (1, 2, 3, 4, 5, 6)  # note that I only need one die_sides, and I can loop over it three times.

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:

die_sides = (1, 2, 3, 4, 5, 6)  # note that I only need one die_sides, and I can loop over it three times.

def roll_dice():
    counts = [0] * 19    # makes a list of 19 zeros.
    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   # adds one to the count at that point in the list.
    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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Oh, you can also use itertools to simplify this quite a bit:

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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(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:

die_sides = (1, 2, 3, 4, 5, 6)  # note that I only need one die_sides, and I can loop over it three times.

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:

die_sides = (1, 2, 3, 4, 5, 6)  # note that I only need one die_sides, and I can loop over it three times.

def roll_dice():
    counts = [0] * 19    # makes a list of 19 zeros.
    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   # adds one to the count at that point in the list.
    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 !
Reply
#5
(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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Money conversion - problems with lists and .format function fatherted99 1 1,784 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  function method problems drchar 2 2,856 Dec-11-2018, 02:38 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020