Introduction: Lucky 3s

In this guide, I will explain and illustrate how to code a Lucky 3s game in python.

Supplies

A computer with Python installed and IDLE

Step 1: Import Random and Create the Function

In order to create the Lucky 3s game, you will need to import random which will allow us to get a random set of numbers, this will be used later in the code. You will also need to create a function to allow the game to be called. We can simply call the function "def lucky3Game():".

Step 2: Greeting the Player

When you first call the function, you will want to great the player. This can be done by simply printing a statement for example 'Welcome to the Lucky Threes Game'). When greeting the player you're also gong to create a counter variable to keep track of their total bet amount. In this example we created the counter variable 'total' and gave it a value of 0.

Step 3: Making the Game Playable

In order to make the game playable for the user we will have to incorporate a loop. In this example we are using a while loop and setting it to true, this allows the game to be run an unlimited amount of times until it is broke which we will introduce in Step 4. While the loop is running, it will print a statement asking the user to input the amount they would like to bet or X to exit the loop. For example this code asks ''How much do you want to bet or X to exit: '. That value inputted will be assigned to the variable user.

Step 4: Breaking the Loop

This area of code will break the loop and end the game. The code checks what the user inputted in the if statement. if the user input is 'X' or 'x' the loop will break and the game will end. A message will be printed once the game ends, this will be shown in Step 12.

Step 5: Creating the List and Count Variables

After we created our if statement that ends the game, we will need to create two new variables outside this statement. The first variable is a list, the list can be assigned to any name, for example we used lst, and it will hold all the values the dice rolled. The second variable we will create is a second count variable. This count variable can be named anything, in the example the name count is used, and it will keep track of the amount if threes the dice rolled.

Step 6: Creating the Loop

Now you will create a for loop to limit the amount of times the dice can roll. You need to assign a variable to keep track of the amount of times the dice was rolled, in the example we use i, and we have the loop in range of 5 so it will only run 5 times.

Step 7: What Goes Inside the Loop

After we create the loop, we now need to put tasks inside the loop. First we will start off by rolling the dice, to do so we have to create a new variable, in the example we used dice, and call upon the random.randrange() method. This method will give us a random number from the range of numbers we enter. Since we are playing a dice game the maximum number we can do is 6. However with randrange() the maximum number we input is not included so we will put the number 7 as the parameter. After we roll the dice we will then append the values we get into the list we created with the append() method.

Step 8: Checking for 3s

We will also include another if statement within the loop. This if statement will check if the dice rolled a three. If the dice rolled a three, we will add one to the count variable. This will keep track of the threes we rolled in the loop.

Step 9: After the Loop

After the loop is executed, we will need a print statement to print what was rolled by the dice. In order to do this we will use print() and within print we will put some text, in the example we used 'Rolls: ', as well as call the lst variable to display the numbers rolled.

Step 10: Checking If the User Won

In order to check if the user won, we will need to construct an if statement. This if statement will check if the count variable, which keeps track of the number of threes, is less than two. If the count value is less than two then the user loss, the total variable will be subtracted by the user variable(the amount the user wanted to bet). The total loss will be shown in a print statement following some text, for example 'You lose! Total winning or loss: $'.

Step 11: Checking If the User Won Part Two

Now if the user won, we will run the else statement. Within the else statement, we will add the user input(the amount the user bet) to the total variable. The total won will be shown in a print statement following some text, for example 'You win! Total winning or loss: $'.

Step 12: Closing Out the Game

Finally in order to close up the code and close the game you will need to enter your final print statement. This statement will be placed outside the while loop and will be printed once the user enters x or X for their "user" value. The statement should also print the total won or lost. The statement should look like this "Thanks for playing. Total winning or loss is $" and print the total.

Step 13: Now the Fun Part

Now that we have finally completed the code portion we can play our game. Simply run the code and enter our function name(lucky3Game():) into the shell. Once u see "Welcome to the Lucky Threes Game" you'll know you did it right. Have fun and enjoy.