Okapi and Preparing for the Bloomsburg Competition

Hello all!

I’ve been on a bit of a ‘hiatus’ lately, I’ve been busy with life things and haven’t had a chance to work on any posts here.  But a quick update, I won first place at regionals for the Pennsylvania Junior Academy of Science so I’m going to states in May and I’ll be making a post on that project soon.  I’ve also been preparing the programming club at my school for an upcoming competition at Bloomsburg University where we will be competing.  Because of this we have been doing practice problems and so I will be posting and explaining my solutions to them here.

Our first practice problem is called Okapi.  The problem description goes as follows:

The game of Okapi is played by rolling three dice. A payout in dollars is determined by the rolled numbers according to the following rule:

  • If the three numbers are the same, the player wins the sum of those three numbers.
  • If only two of the numbers are the same, the player wins the sum of the two equal numbers.
  • For three different numbers, the player wins nothing.

Write a program that prompts the user for three dice rolls and outputs the payout.

We need to begin this problem by taking user input using rolls = input("Enter dice rolls: ") which prompts the user for input and sets rolls equal to their input. Next we need to parse out their answer into three separate rolls, this is rather easy and just a matter of indexing the user input. In order to do this we just need to create variables for each roll and then set them to the correct index of rolls using the following code: roll_one, roll_two, roll_three = int(rolls[0]), int(rolls[1]), int(rolls[2]). Now that we have our rolls assigned we just need to use a bunch of conditional statements to determine the output.  Our final code will look like this:

def okapi():
    rolls = input("Enter dice rolls: ")
    roll_one, roll_two, roll_three = int(rolls[0]), int(rolls[1]), int(rolls[2])
    if roll_one == roll_two and roll_two == roll_three:
        print("The payout is $", roll_one*3, ".")
    elif roll_one == roll_two:
        print("The payout is $", roll_one+roll_two, ".")
    elif roll_two == roll_three:
        print("The payout is $", roll_two+roll_three, ".")
    elif roll_one == roll_three:
        print("The payout is $", roll_one+roll_three, ".")
    else:
        print("The payout is $0.")

And now we have a working solution to problem #1!  Another solution can be found on my GitHub, it’s the same premise but just less readable.  I’ll most likely be posting around weekly again soon.

Thanks for reading and have a wonderful day!
~ Corbin