Assignment 4 - ldkvd/CS101L GitHub Wiki

Welcome to the CS101L Wiki!

Functions make it much easier to debug a program as well. You can call the function from the command line multiple times to make sure it functions correctly. Once you are satisfied you can call the functions in your program and be confident that the results will be consistent.

In this program, you are going to simulate a slot machine in Pierson Hall. The slot machine has 3 reels and each reel will have numbers ranging from 1 to 10. The user can choose how many chips they start out with. They are allowed to wager between 1 to the amount of bank they currently have on each spin. If they match 3 numbers, they will win 10 times their wager. If they match 2 numbers they will win 3 times their wager

Slot_UnitTests.py

Slot_UnitTests will run your functions and check for valid results. They don’t test everything, but if you get all the tests passing then you'll have a pretty good indication that you’ve written most of the functions effectively. You can edit and run the Slot_UnitTests.py in python just like your program. However, make sure you save your lab05.py program before you run the tests. It can’t tell that you’ve got some unsaved changes in the editor.

play_again() function

The play_again () function will ask the user if they want to play again. It will return True if the user enters Y or YES regardless of the case. The function will return False if the user enters N or NO. If the user enters anything else then it will warn the user and ask them to enter another value.

get_wager(bank : int) -> int:function

This function should return an integer. It asks the user how many chips they want to wager. It must be greater than 0, and less than or equal to the bank. Otherwise, the user gets warned and they are prompted until they enter a valid value.

get_slot_results() -> tuple: function

This returns 3 random reels at once. It returns them as a tuple. Notice you can return 3 integers all In the same return-separated by a comma. Notice in the main program we can assign them to 3 variables when the function is called. Each reel is a random number between 1 and 10 inclusive.

get_matches(reela, reelb, reelc) -> int:function

This function returns 3 if all 3 reels match, 2 if there are 2 matching reels, and 0 otherwise.

get_bank() -> int:function

Ask the user for how many chips they want to start with. Must be greater than 0 and less than 101. If not valid entry then it will warn the user and keep asking until they enter valid input and return the valid input

get_payout(wager, matches):function

Returns the payout based on the # of matches and the wager. It assumes that the bank did not automatically initially subtract the wager. If matches are 3, then the payout is 10 times the wager and subtract the wager. If matches are 2 then the payout is 3 times the wager and subtract the wager. If matches is 0, then the payout is the negative of the wager.

Answers: Image 1 Image 2 Image 3 Image 4 Image 5

In this code, multiple functions are defined and called. Functions allow for the same piece of code to run multiple times. It reduces clutters, complexity, and duplication of codes. It breaks down a large program into smaller, easy-to-manage components. Especially in a situation like this stimulation of a slot machine where we use similar codes to output multiple reels, calculate the wagers, asks for user input, etc. for each spin and game, we can minimize redundancy. We can call one statement (the function), rather than writing the same code over and over for each spin/game.

The function play_again() will ask the user if they want to play again. It will run through the if statements and return the respective result. The function get_wager(bank) will ask the user how many chips they want to wager and return the value. The function get_slot_results() will generate and return three random integers with values between and including 1 and 10. The function get_matches(reela, reelb, reelc) will enter the if-else statements to compare the three random reels for matches and return their respective value. The function get_bank() will as the user how many chips they want to start the game with. The function get_payout(wager, matches) will calculate and return the payout based on the number of matches and the amount of the wager.

In the main program, an outer while loop is called with other statements are nested inside of it. When while playing is true, all the different functions are called to perform the different actions and return the different values. A 'i' variable used is to iterate and count the number of spins it takes to lose all the money. The program will display how many rounds it took for the user to lose all of their chips and the most amount of chips they acquired in the game.