Search code examples
pythondicerolling-computation

I am new, trying to roll 3d6, rerolling 1's, and put them into a list. I cant seem to get 3 "new" values in the list. The numbers just keep adding up


import random

my_stats = []

stat = 0

while len(my_stats) < 3:
    for i in range(1,4):                # generates a num (d6) but no 1's
        number = (random.randint(1, 6))
        while number == 1:
            number = (random.randint(1, 6))
        else:
            stat += number          # sums the 3 rolls into stat

    my_stats.append(stat)

print(my_stats)

I am not sure how to get 3 distinct entries in my list, just keeps adding up.


Solution

  • Your code currently rolls a die 3 times, adding the results if the roll is not 1, otherwise rerolling the die pointlessly (as the value is never used). The summed value is then appended to a list and you repeat the process, but without resetting the sum to 0 first. So it contains at least 2 errors (as ignoring the 1 and not using the reroll doesn't appear to be what you want)

    I think this is what you were after:

    import random
    
    my_stats = []
    
    stat = 0
    
    while len(my_stats) < 3:
        # you'd want to restart the count every time
        stat = 0
        # you'll need to keep track of the number of good rolls, can roll any number of 1's
        good_rolls = 0
        while good_rolls < 3:
            number = (random.randint(1, 6))
            if number != 1:
                stat += number          # sums the 3 rolls into stat
                good_rolls += 1
    
        my_stats.append(stat)
    
    print(my_stats)