dimanche 27 septembre 2020

Using list instead of dictionary for organized results

I'm trying to get my code below working to have the results organized rather than random.

This is what's happening now.

sum = 9 count = 117
sum = 6 count = 142
sum = 3 count = 58
sum = 7 count = 172
sum = 8 count = 129
sum = 5 count = 109
sum = 4 count = 87
sum = 11 count = 53
sum = 12 count = 31
sum = 10 count = 72

And what I'm trying to achieve is

    sum = 1 count = 117
    sum = 2 count = 142
    sum = 3 count = 58
    sum = 4 count = 172
    sum = 5 count = 129
    sum = 6 count = 109
    sum = 7 count = 87
    sum = 8 count = 53
    sum = 12 count = 31

etc. While omitting any number that hasn't been rolled. I'd ideally like to use a list instead of a dictionary but any time I try it I get varying errors. Currently this outputs the amount but not in order.

    import random
    
    print("results")
    occurrences = []
    for i in range(1000):
        die1 = random.randint(1, 6)
        die2 = random.randint(1, 6)
        roll = die1 + die2
        current = occurrences[roll, ]
        occurrences[roll] = current + 1
    
    for roll, count in occurrences[]
        print(f"sum = {roll} count = {count}")



Aucun commentaire:

Enregistrer un commentaire