0
import random
#Making sure all values are = to 0
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0

#Loop for rolling the die 100 times
for r in range(0, 100):
    roll = random.randint(1, 6)
    if roll == 1:
        one += 1
    elif roll == 2:
        two += 1
    elif roll == 3:
        three += 1
    elif roll == 4:
        four += 1
    elif roll == 5:
        five += 1
    elif roll == 6:
        six += 1

#print how many times each number was rolled
print(one)
print(two)
print(three)
print(four)
print(five)
print(six)

#How many times the 3 was rolled
print("The 3 was rolled", three, "times!")

#Average roll between all of them
print("The average roll was", (one * 1 + two * 2 + three * 3 + 4 * four + 5 * five + 6 * six)/100)

I am trying to make it so it prints out

"number is the most common roll." for whichever the roll is.

Just trying to do this is the most simple way, and I'm confused on how to do it. I tried to do like if one > two > three etc etc. but that did not work.

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
BRyder18
  • 11
  • 1
  • 4

1 Answers1

1

Choosing an appropriate data structure to gain maximum leverage of a language’s core features is one of the most valuable skills a programmer can develop.

For this particular use case, it’s best to use an iterable data type that facilitates operations (e.g. sort, obtain the maximum) on the collection of numbers. As we want to associate a number (1-6) with the number of times that number was rolled, a dictionary seems like the simplest data structure to choose.

I’ve re-written the program to show how its existing functionality could be re-implemented using a dictionary as its data structure. With the comments, the code should be fairly self-explanatory.

The tricky part is what this question is actually asking: determining the number rolled most often. Instead of manually implementing a sorting algorithm, we can use the Python built-in max function. It can accept an optional key argument which specifies the function that should be applied to each item in the iterable object before carrying out the comparison. In this case, I chose the dict.get() method which returns the value corresponding to a key. This is how the dictionary of roll results is compared by the number of rolls for each result for determining the number rolled most often. Note that max() only returns one item so in the case of a tie, only one of the results will be printed (see Which maximum does Python pick in the case of a tie?).

See also: How do I sort a dictionary by value?

import random

NUM_ROLLS = 100
DIE_SIDES = 6

# Create the dictionary to store the results of each roll of the die.
rolls = {}

#Loop for rolling the die NUM_ROLLS times
for r in range(NUM_ROLLS):
    roll_result = random.randint(1, DIE_SIDES)
    if roll_result in rolls:
        # Add to the count for this number.
        rolls[roll_result] += 1
    else:
        # Record the first roll result for this number.
        rolls[roll_result] = 1

# Print how many times each number was rolled
for roll_result in range(1, 7):
    print("The number", str(roll_result), "was rolled", str(rolls[roll_result]), "times.")

#How many times the 3 was rolled
print("The number three was rolled", str(rolls[3]), "times.")

#Average roll between all of them
sum = 0
for roll_result in rolls:
    sum += roll_result * rolls[roll_result]
print("The average roll result was", str(sum/NUM_ROLLS))

# The number rolled most often.
print(str(max(rolls, key=rolls.get)), "is the most common roll result.")
Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56