Write a menu driven program called assign2 _ yourEmailId.py that will allow the user to enter commands and process these commands until the quit command is entered. The program will store and maintain player information (using a List of Lists). Player information will be stored in a text file that will be read in when the program commences. Once the initial player data has been read in from the file, the program should allow the user to interactively query and manipulate the player information. Input When your program begins, it will read in player information from a file called players.txt. This is a text file that stores information pertaining to players. An example input file called players.txt can be found on the course website (under the Assessment tab). You may assume that all data is in the correct format. The name of the player is stored on a separate line. The very next line contains the number of games played, games won, games lost, games drawn, chip balance and the total score. This information is stored on one line and is separated by the space character as seen in Figure 1 below: After the program has stored the data (using a List of Lists), it will enter interactive mode as described in the following section. Bruce Wayne 5 5 0 0 100 15 Jessica Jones 12 0 6 6 10 6 Johnny Rose 6 2 0 4 20 10 Gina Linetti 7 4 0 3 300 15 Buster Bluth 3 0 2 1 50 1 Figure 1: Player information file format (players.txt). Your program will maintain one List of Lists as follows: player_list = [] # List of Lists to store player information Once the above information is read in from the file, the player list will be populated as follows: player_list 0 [Bruce Wayne, 5, 5, 0, 0, 100, 15] 1 [Jessica Jones, 12, 0, 6, 6, 10, 6] 2 [Johnny Rose, 6, 2, 0, 4, 20, 10] 3 [Gina Linetti, 7, 4, 0, 3, 300, 15] 4 [Buster Bluth, 3, 0, 2, 1, 50, 1] Note: A player and their statistics are stored as a list and are stored as one item in the player list, i.e. a List of Lists (as seen above). 6 of 33 Interactive Mode Your program should enter an interactive mode after the player information has been read from the file. The program will allow the user to enter commands and process these commands until the quit command is entered. The following commands should be allowed: 1. list: Displays for all players, the player’s name, games played, won, lost, drawn, number of chips and their total score. Outputs the contents of the player list as seen below in the section titled Screen Format. Please read the section at the end of this handout titled – 'Useful Built-In Python Functions'. 2. buy: Prompts for and reads the player’s name and searches for the player in the list of players (list of lists). If the player is found in the list of players, the number of chips the player would like to buy is prompted for and read (the player can only buy between 1-100 chips inclusive at a time). The player’s chip balance is updated accordingly and a message indicating that this has been done is displayed to the screen. If the player is not found in the player list, an error message is displayed. 3. search: Prompts for and reads the player’s name and searches for the player in the player list. If the player is found in the player list, the player’s name, games played, won, lost, drawn, number of chips and their total score, are displayed to the screen as seen below (in the section titled Screen Format). If the player is not found in the list of players, an error message stating the player has not been found is displayed to the screen. Please read the section at the end of this handout titled – 'Useful Built-In Python Functions'. 4. high: Displays the player with the highest chip balance in the list of players. Where two players have the same chip balance, the player with the lower games played value should be displayed to the screen - see section titled Screen Format below. If no players are stored in the list or a player with a highest chip balance cannot be found (i.e. all players have a chip balance of zero), display an error message accordingly. 5. add: Prompts for and reads the player’s name. If the player does not already exist (i.e. a match is not found on the player’s name), the player is added to the list of players. If the player is added, chip balance is initialised to 100 and all other data members (games played, games won, games lost, games drawn, and the player’s total score), are initialised to zero and a message is displayed to the screen indicating that this has been successfully added. The player information must be added after the last player entry stored in the list (i.e. at the end of the list). If the player is already stored in the player list, an error message is displayed. No duplicate entries are allowed. 6. remove: Prompts for the player’s name. If the player is found, he/she is removed from the list of players and a message is displayed to the screen indicating that this has been done. If the player is not found in the player list, an error message is displayed. 7. play: Prompts for the player’s name. The program searches for the player in the list of players and if the player is not found in the list of players, an error message is displayed to the screen. If the player is found in the list of players, he/she is then able to play Blackjack against the computer until he/she does not wish to continue playing (enters ‘n’ when prompted to ‘Play again (y|n)?’). 7 of 33 After every game, the player’s game statistics are updated (i.e. games played, total score, etc…). Three (3) points are awarded if the player wins, zero (0) points are awarded if the player loses and one (1) point is awarded if the player draws with the dealer. The player’s chip balance is also updated. If the player wins, the player’s chip balance will be increased by the amount of chips bet. If the player loses, the player’s chip balance will be decreased by the amount of chips bet. A push (draw) result does not change the player’s chip balance in any way. 8. chips: Displays the list of players in descending order of chips. Where two players have the same number of chips, the player with the lower games played value should appear first. This command should not alter the original list of players in any way. The information is displayed to the screen as described in the section titled 'Screen Format' below. 9. quit: Causes the program to quit, outputting the contents of the player list (List of lists) to a file (see section ‘Final Output’ below for format). Note: The program should display an appropriate message if a player is not found matching a search criteria. Appropriate messages should also be displayed to indicate whether a command has been successfully completed. Please refer to the sample output (at the end of this handout) to ensure that your program is behaving correctly and that you have the correct output messages. Each time your program prompts for a command, it should display the list of available commands. See the sample output (at the end of this handout) to ensure that you have the output format correct. For example: Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: Menu input should be validated with an appropriate message being displayed if incorrect input is entered. 8 of 33 Screen Format The list command (display_players() function) should display the player information in the following format: =========================================================== - Player Summary - =========================================================== - P W L D Chips Score - ----------------------------------------------------------- - Bruce Wayne 5 5 0 0 100 15 - ----------------------------------------------------------- - Jessica Jones 12 0 6 6 10 6 - ----------------------------------------------------------- - Johnny Rose 6 2 0 4 20 10 - ----------------------------------------------------------- - Gina Linetti 7 4 0 3 300 15 - ----------------------------------------------------------- - Buster Bluth 3 0 2 1 50 1 - ----------------------------------------------------------- =========================================================== Likewise for the chips command (which should use the same display_players() function) should display the player information (sorted on chips) in the following format: =========================================================== - Player Summary - =========================================================== - P W L D Chips Score - ----------------------------------------------------------- - Gina Linetti 7 4 0 3 300 15 - ----------------------------------------------------------- - Bruce Wayne 5 5 0 0 100 15 - ----------------------------------------------------------- - Buster Bluth 3 0 2 1 50 1 - ----------------------------------------------------------- - Johnny Rose 6 2 0 4 20 10 - ----------------------------------------------------------- - Jessica Jones 12 0 6 6 10 6 - ----------------------------------------------------------- =========================================================== The search command should display individual player information to the screen in the following format: Johnny Rose stats: P W L D Score 6 2 0 4 10 Chips: 20 The high command (display _highest_chip_holder() function) should display the player with the highest number of battles won in the following format: Highest Chip Holder => Gina Linetti with 300 chips! Final Output When your program finishes (because you entered the quit command) your program should output the contents of the list of players to a file called new _ players.txt. The format of this file should exactly match that of the input file. 9 of 33 PRACTICAL REQUIREMENTS It is recommended that you develop this part of the assignment in the suggested stages. It is expected that your solution WILL include the use of: • Your solution in a file called assing2_yourEmailId.py. • The supplied blackjack.py module (that plays one game of Blackjack (dice) against the computer). This is provided for you – do NOT modify this file. • Use of the supplied play_one_game(no_chips) function from the blackjack.py module. You are not required to implement the play_one_game(no_chips) function or the blackjack.py module. This is provided for you (blackjack.py module) – do NOT modify this file. • Appropriate and well constructed while and/or for loops. (Marks will be lost if you use break statements or the like in order to exit from loops). • A list of lists to store player information. • You must implement each function listed below. • Appropriate if, if-else, if-elif-else statements (as necessary). • The following ten functions: o read_file(filename) This function takes a file name and reads the contents of that file into a list called player_list. The function returns the newly created list of players (i.e. a list of lists). You must use a loop in your solution. You may use String and/or List methods in this function only. You may find the String methods split() and strip() useful here. Please note: This function will be provided for you. You may use your own function or use the function provided… the decision is yours. : ) o write_to_file(filename, player_list) This function will output the contents of the player list (list of list) to a file in the same format as the input file. The file will need to be opened for writing in this function (and of course closed once all writing has been done). The function accepts the filename of the file to write to and the list of players. You must use a loop in your solution. o display_players(player_list) This function will take the list of players (list of lists) as a parameter and will output the contents of the list to the screen. This function displays the information to the screen in the format specified in the assignment specifications under the section - 'Screen Format'. You must use a loop in your solution. Please have a read of the section at the end of this handout titled – 'Useful Built-In Python Functions'. o find_player(player_list, name) This function will take the player’s name as input along with the list of players (player list of lists) and will return the position (index) of the player found in the player_list. If the player is not found, the function returns -1. You must use a loop in your solution. You must not use list methods in your solution. o buy_player_chips(player_list, name) This function takes the player list (list of lists) and the player's name and updates the player's chip balance. If the player is found, the function prompts for and reads the number of chips the player would like to buy (between 1-100) and updates the player's chip balance by that amount. A message indicating that this has been done is displayed to the screen. You should validate the number of chips to ensure the player only enters between 1-100. If the player is not found in the list of players, an error message is displayed to the screen. You must call function find_player() from this function. 10 of 33 o display_highest_chip_holder(player_list) This function takes the player list (list of lists) as a parameter and displays the player with the highest chip balance in the list of players to the screen. Where two players have the same chip balance, the player with the lower games played value should be displayed to the screen. If no players are stored in the list or a player with a highest chip balance cannot be found (i.e. all players have a chip balance of zero), display an error message accordingly. o add_player(player_list, name) This function takes the list of players (list of lists) and the player’s (to be added) name, as parameters. If the player already exists in the list of players, display an error message to the screen. If the player does not exist in the player list (list of lists), the player is added. Create a new list with the information read in (i.e. name) and add the player’s information (as a list) to the end of the list of players (list of lists). If the player is added, chip balance is initialised to 100 and all other stats data (games played, no won, no lost, no drawn and the player’s total score), are initialised to zero and a message is displayed to the screen indicating this has been successfully added. This function returns the list of players (list of lists). You may use the list_name.append(item) method to add the new player to the list of players. You must call function find_player() from this function. o remove _player(player_list, name) This function takes the list of players (list of lists) and the player’s (to be removed) name as parameters. If the player is not found in the list of players, display an error message to the screen. If the player does exist in the players list, this function removes the player and displays a message to the screen indicating that the player has been removed from the players list (list of lists). This function returns the list of players (list of lists). You may use the list_name.append(item) method in this function. You must call function find_player() from this function. o play_blackjack_games(player_list, player_pos) This function allows a player to play Blackjack against the computer until he/she does not wish to continue playing (enters ‘n’ when prompted to ‘Play again [y|n]?’). After every game, the player’s chip balance and game stats are updated. 3 points are awarded if the player wins, 0 points are awarded if the player loses and 1 point is awarded if the player draws. The function takes the player list (list of lists) and the player’s position (index) in the player list as parameters. This function calls function play_one_game(no_chips) (i.e. the function provided for you in the blackjack module; see note below). You must use a loop in your solution. Please note: You DO NOT have to write your own game of Blackjack. One has been provided for you. The blackjack.py file contains the function called play_one_game(no_chips) that allows you to play one game of Blackjack (dice version) against the computer. Please do not modify this module. o sort_by_chips (player_list) This function takes the list of players (list of lists) and returns a copy of the list in descending order of chip balance. Where two players have the same chip balance, the player with the lower number of games played value should appear first. This function must NOT modify the player list being passed in as a parameter. Your solution must NOT make use of List methods or any of the sort functions available in the Python Standard Library. This function returns a copy of the player list (list of lists) sorted in descending order of chip balance. You may wish to create/define additional functions (which are called from within this function) to assist with this task. • Good programming practice: o Consistent commenting, layout and indentation. You are to provide comments to describe: your details, program description, all variable definitions, all function definitions and every significant section of code. o Meaningful variable names. 11 of 33 Your solutions MAY make use of the following: • Built-in functions int(), input(), print(), range(), open(), close(), len(), format() and str(). • Concatenation (+) operator to create/build new strings. • The list_name.append(item) method to update/create lists. • Access the individual elements in a string with an index (one element only). i.e. string_name[index]. • Access the individual elements in a list with an index (one element only). i.e. list_name[index] or list_name[i][j]. Your solutions MUST NOT use: • Built-in functions (other than the int(), input(), print(), range(), open(), close() len(), format() and str() functions). • Slice expressions to select a range of elements from a string or list. i.e. name[start:end]. • String or list methods (i.e. other than those mentioned in the 'MAY make use' of section above and those needed for the read_file() function). • Global variables as described in week 10 material. • The enumerate() function. • List comprehensions. • Dictionary to store data items. • Do not use break, or continue statements in your solution – doing so will result in a significant mark deduction. Do not use the return statement as a way to break out of loops. Do not use quit() or exit() functions as a way to break out of loops. PLEASE NOTE: You are reminded that you should ensure that all input and output conform to the assignment specifications. If you are not sure about these details you should check with the sample output provided at the end of this document or post a message to the discussion forum in order to seek clarification. Please ensure that you use Python 3.12.2 or a later version (i.e. the latest version) in order to complete your assignments. Your programs MUST run using Python 3.12.2 (or latest version). 12 of 33 STAGES It is recommended that you develop this part of the assignment in the suggested stages. Many problems in later stages are due to errors in early stages. Make sure you have finished and thoroughly tested each stage before continuing. The following stages of development are recommended: Stage 1 To begin, download the provided files (available on the course website – Assessment Tab) and re-name assign2_yourEmailId.py to assign2 _ yourEmailId.py. i.e. assign2_bonjy007.py Define an empty list to store the player information. For example: player_list = [] Call function play_one_game() (provided for you in the blackjack.py module). Note: you may simply download it from the course website under the Assessment tab. The file blackjack.py should be placed in the same directory as your assign2_yourEmailId.py file. Please do NOT modify the blackjack.py module. Import the blackjack module and call function play_one_game() as seen below. You may wish to read the material on modules posted on the course website (under the assessment tab). import blackjack player_list = [] no_chips = 100 game_result = 0 # Plays one game of blackJack and assigns the result of the game and the chip balance # to variables game_result and no_chips respectively. game_result, no_chips = blackjack.play_one_game(no_chips) # Display to the screen the result of play_one_game() –game result (value of 3, 1 or 0) # and number of chips remaining. print(game_result, no_chips) Make sure the program runs correctly. Once you have that working, back up your program. Note: When developing software, you should always have fixed points in your development where you know your software is bug free and runs correctly. Once you are sure that your program is working correctly, you can either remove or comment out the above statements – we just wanted to make sure it was working correctly! You can bring it back later (appropriately positioned). Stage 2 Write the code for functions read_file() and display _ players() as specified above. Add code to call these two functions to ensure they are working correctly. You may write your own read_file() function or you may use the one provided for you (included in the provided file). 13 of 33 Stage 3 Now that you know the information is being correctly stored in your players list, write the code for function write_to_file(). Add code to call this function to ensure it is working correctly. Stage 4 Implement the interactive mode, i.e. to prompt for and read menu commands. Set up a loop to obtain and process commands. Test to ensure that this is working correctly before moving onto the next stage. You do not need to call any functions at this point, you may simply display an appropriate message to the screen, for example: Sample output: Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: display Not a valid command - please try again. Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: list In list command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: buy In buy command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: search In search command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: high In high command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: add In add command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: remove In remove command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: play In play command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: chips In chips command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: quit Menu input should be validated with an appropriate message being displayed if incorrect input is entered by the user. 14 of 33 Stage 5 Implement one command at a time. Test to ensure the command is working correctly before starting the next command. Start with the quit and list commands as they do not need you to add anything further to the file other than ensuring that the function calls are in the correct place. You should be able to see that for most commands there is a corresponding function(s). For the remaining commands, the following implementation order is suggested (note: this is a guide only): • list command (display_players() function). • search command (find_player() function). • add command (add_player() function). • buy command (buy_player_chips() function). • remove command (remove_player() function). • high command (display_highest_chip_holder() function). • play command (play_blackjack_games() function). • chips command (sort_by_chips() function). Stage 6 Ensure that you have validated all user input with an appropriate message being displayed for incorrect input entered by the user. Add code to validate all user input. Hint: use a while loop to validate input. Stage 7 Finally, check the sample output (see section titled ‘Sample Output’ towards the end of this document) and if necessary, modify your code so that: • The output produced by your program EXACTLY adheres to the sample output provided. • Your program behaves as described in these specs and the sample output provided.
function that: add two numbers together ```python def add(a, b): return a + b add(1, 2) ``` function that: Write a menu driven program called assign2 _ yourEmailId.py that will allow the user to enter commands and process these commands until the quit command is entered. The program will store and maintain player information (using a List of Lists). Player information will be stored in a text file that will be read in when the program commences. Once the initial player data has been read in from the file, the program should allow the user to interactively query and manipulate the player information. Input When your program begins, it will read in player information from a file called players.txt. This is a text file that stores information pertaining to players. An example input file called players.txt can be found on the course website (under the Assessment tab). You may assume that all data is in the correct format. The name of the player is stored on a separate line. The very next line contains the number of games played, games won, games lost, games drawn, chip balance and the total score. This information is stored on one line and is separated by the space character as seen in Figure 1 below: After the program has stored the data (using a List of Lists), it will enter interactive mode as described in the following section. Bruce Wayne 5 5 0 0 100 15 Jessica Jones 12 0 6 6 10 6 Johnny Rose 6 2 0 4 20 10 Gina Linetti 7 4 0 3 300 15 Buster Bluth 3 0 2 1 50 1 Figure 1: Player information file format (players.txt). Your program will maintain one List of Lists as follows: player_list = [] # List of Lists to store player information Once the above information is read in from the file, the player list will be populated as follows: player_list 0 [Bruce Wayne, 5, 5, 0, 0, 100, 15] 1 [Jessica Jones, 12, 0, 6, 6, 10, 6] 2 [Johnny Rose, 6, 2, 0, 4, 20, 10] 3 [Gina Linetti, 7, 4, 0, 3, 300, 15] 4 [Buster Bluth, 3, 0, 2, 1, 50, 1] Note: A player and their statistics are stored as a list and are stored as one item in the player list, i.e. a List of Lists (as seen above). 6 of 33 Interactive Mode Your program should enter an interactive mode after the player information has been read from the file. The program will allow the user to enter commands and process these commands until the quit command is entered. The following commands should be allowed: 1. list: Displays for all players, the player’s name, games played, won, lost, drawn, number of chips and their total score. Outputs the contents of the player list as seen below in the section titled Screen Format. Please read the section at the end of this handout titled – 'Useful Built-In Python Functions'. 2. buy: Prompts for and reads the player’s name and searches for the player in the list of players (list of lists). If the player is found in the list of players, the number of chips the player would like to buy is prompted for and read (the player can only buy between 1-100 chips inclusive at a time). The player’s chip balance is updated accordingly and a message indicating that this has been done is displayed to the screen. If the player is not found in the player list, an error message is displayed. 3. search: Prompts for and reads the player’s name and searches for the player in the player list. If the player is found in the player list, the player’s name, games played, won, lost, drawn, number of chips and their total score, are displayed to the screen as seen below (in the section titled Screen Format). If the player is not found in the list of players, an error message stating the player has not been found is displayed to the screen. Please read the section at the end of this handout titled – 'Useful Built-In Python Functions'. 4. high: Displays the player with the highest chip balance in the list of players. Where two players have the same chip balance, the player with the lower games played value should be displayed to the screen - see section titled Screen Format below. If no players are stored in the list or a player with a highest chip balance cannot be found (i.e. all players have a chip balance of zero), display an error message accordingly. 5. add: Prompts for and reads the player’s name. If the player does not already exist (i.e. a match is not found on the player’s name), the player is added to the list of players. If the player is added, chip balance is initialised to 100 and all other data members (games played, games won, games lost, games drawn, and the player’s total score), are initialised to zero and a message is displayed to the screen indicating that this has been successfully added. The player information must be added after the last player entry stored in the list (i.e. at the end of the list). If the player is already stored in the player list, an error message is displayed. No duplicate entries are allowed. 6. remove: Prompts for the player’s name. If the player is found, he/she is removed from the list of players and a message is displayed to the screen indicating that this has been done. If the player is not found in the player list, an error message is displayed. 7. play: Prompts for the player’s name. The program searches for the player in the list of players and if the player is not found in the list of players, an error message is displayed to the screen. If the player is found in the list of players, he/she is then able to play Blackjack against the computer until he/she does not wish to continue playing (enters ‘n’ when prompted to ‘Play again (y|n)?’). 7 of 33 After every game, the player’s game statistics are updated (i.e. games played, total score, etc…). Three (3) points are awarded if the player wins, zero (0) points are awarded if the player loses and one (1) point is awarded if the player draws with the dealer. The player’s chip balance is also updated. If the player wins, the player’s chip balance will be increased by the amount of chips bet. If the player loses, the player’s chip balance will be decreased by the amount of chips bet. A push (draw) result does not change the player’s chip balance in any way. 8. chips: Displays the list of players in descending order of chips. Where two players have the same number of chips, the player with the lower games played value should appear first. This command should not alter the original list of players in any way. The information is displayed to the screen as described in the section titled 'Screen Format' below. 9. quit: Causes the program to quit, outputting the contents of the player list (List of lists) to a file (see section ‘Final Output’ below for format). Note: The program should display an appropriate message if a player is not found matching a search criteria. Appropriate messages should also be displayed to indicate whether a command has been successfully completed. Please refer to the sample output (at the end of this handout) to ensure that your program is behaving correctly and that you have the correct output messages. Each time your program prompts for a command, it should display the list of available commands. See the sample output (at the end of this handout) to ensure that you have the output format correct. For example: Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: Menu input should be validated with an appropriate message being displayed if incorrect input is entered. 8 of 33 Screen Format The list command (display_players() function) should display the player information in the following format: =========================================================== - Player Summary - =========================================================== - P W L D Chips Score - ----------------------------------------------------------- - Bruce Wayne 5 5 0 0 100 15 - ----------------------------------------------------------- - Jessica Jones 12 0 6 6 10 6 - ----------------------------------------------------------- - Johnny Rose 6 2 0 4 20 10 - ----------------------------------------------------------- - Gina Linetti 7 4 0 3 300 15 - ----------------------------------------------------------- - Buster Bluth 3 0 2 1 50 1 - ----------------------------------------------------------- =========================================================== Likewise for the chips command (which should use the same display_players() function) should display the player information (sorted on chips) in the following format: =========================================================== - Player Summary - =========================================================== - P W L D Chips Score - ----------------------------------------------------------- - Gina Linetti 7 4 0 3 300 15 - ----------------------------------------------------------- - Bruce Wayne 5 5 0 0 100 15 - ----------------------------------------------------------- - Buster Bluth 3 0 2 1 50 1 - ----------------------------------------------------------- - Johnny Rose 6 2 0 4 20 10 - ----------------------------------------------------------- - Jessica Jones 12 0 6 6 10 6 - ----------------------------------------------------------- =========================================================== The search command should display individual player information to the screen in the following format: Johnny Rose stats: P W L D Score 6 2 0 4 10 Chips: 20 The high command (display _highest_chip_holder() function) should display the player with the highest number of battles won in the following format: Highest Chip Holder => Gina Linetti with 300 chips! Final Output When your program finishes (because you entered the quit command) your program should output the contents of the list of players to a file called new _ players.txt. The format of this file should exactly match that of the input file. 9 of 33 PRACTICAL REQUIREMENTS It is recommended that you develop this part of the assignment in the suggested stages. It is expected that your solution WILL include the use of: • Your solution in a file called assing2_yourEmailId.py. • The supplied blackjack.py module (that plays one game of Blackjack (dice) against the computer). This is provided for you – do NOT modify this file. • Use of the supplied play_one_game(no_chips) function from the blackjack.py module. You are not required to implement the play_one_game(no_chips) function or the blackjack.py module. This is provided for you (blackjack.py module) – do NOT modify this file. • Appropriate and well constructed while and/or for loops. (Marks will be lost if you use break statements or the like in order to exit from loops). • A list of lists to store player information. • You must implement each function listed below. • Appropriate if, if-else, if-elif-else statements (as necessary). • The following ten functions: o read_file(filename) This function takes a file name and reads the contents of that file into a list called player_list. The function returns the newly created list of players (i.e. a list of lists). You must use a loop in your solution. You may use String and/or List methods in this function only. You may find the String methods split() and strip() useful here. Please note: This function will be provided for you. You may use your own function or use the function provided… the decision is yours. : ) o write_to_file(filename, player_list) This function will output the contents of the player list (list of list) to a file in the same format as the input file. The file will need to be opened for writing in this function (and of course closed once all writing has been done). The function accepts the filename of the file to write to and the list of players. You must use a loop in your solution. o display_players(player_list) This function will take the list of players (list of lists) as a parameter and will output the contents of the list to the screen. This function displays the information to the screen in the format specified in the assignment specifications under the section - 'Screen Format'. You must use a loop in your solution. Please have a read of the section at the end of this handout titled – 'Useful Built-In Python Functions'. o find_player(player_list, name) This function will take the player’s name as input along with the list of players (player list of lists) and will return the position (index) of the player found in the player_list. If the player is not found, the function returns -1. You must use a loop in your solution. You must not use list methods in your solution. o buy_player_chips(player_list, name) This function takes the player list (list of lists) and the player's name and updates the player's chip balance. If the player is found, the function prompts for and reads the number of chips the player would like to buy (between 1-100) and updates the player's chip balance by that amount. A message indicating that this has been done is displayed to the screen. You should validate the number of chips to ensure the player only enters between 1-100. If the player is not found in the list of players, an error message is displayed to the screen. You must call function find_player() from this function. 10 of 33 o display_highest_chip_holder(player_list) This function takes the player list (list of lists) as a parameter and displays the player with the highest chip balance in the list of players to the screen. Where two players have the same chip balance, the player with the lower games played value should be displayed to the screen. If no players are stored in the list or a player with a highest chip balance cannot be found (i.e. all players have a chip balance of zero), display an error message accordingly. o add_player(player_list, name) This function takes the list of players (list of lists) and the player’s (to be added) name, as parameters. If the player already exists in the list of players, display an error message to the screen. If the player does not exist in the player list (list of lists), the player is added. Create a new list with the information read in (i.e. name) and add the player’s information (as a list) to the end of the list of players (list of lists). If the player is added, chip balance is initialised to 100 and all other stats data (games played, no won, no lost, no drawn and the player’s total score), are initialised to zero and a message is displayed to the screen indicating this has been successfully added. This function returns the list of players (list of lists). You may use the list_name.append(item) method to add the new player to the list of players. You must call function find_player() from this function. o remove _player(player_list, name) This function takes the list of players (list of lists) and the player’s (to be removed) name as parameters. If the player is not found in the list of players, display an error message to the screen. If the player does exist in the players list, this function removes the player and displays a message to the screen indicating that the player has been removed from the players list (list of lists). This function returns the list of players (list of lists). You may use the list_name.append(item) method in this function. You must call function find_player() from this function. o play_blackjack_games(player_list, player_pos) This function allows a player to play Blackjack against the computer until he/she does not wish to continue playing (enters ‘n’ when prompted to ‘Play again [y|n]?’). After every game, the player’s chip balance and game stats are updated. 3 points are awarded if the player wins, 0 points are awarded if the player loses and 1 point is awarded if the player draws. The function takes the player list (list of lists) and the player’s position (index) in the player list as parameters. This function calls function play_one_game(no_chips) (i.e. the function provided for you in the blackjack module; see note below). You must use a loop in your solution. Please note: You DO NOT have to write your own game of Blackjack. One has been provided for you. The blackjack.py file contains the function called play_one_game(no_chips) that allows you to play one game of Blackjack (dice version) against the computer. Please do not modify this module. o sort_by_chips (player_list) This function takes the list of players (list of lists) and returns a copy of the list in descending order of chip balance. Where two players have the same chip balance, the player with the lower number of games played value should appear first. This function must NOT modify the player list being passed in as a parameter. Your solution must NOT make use of List methods or any of the sort functions available in the Python Standard Library. This function returns a copy of the player list (list of lists) sorted in descending order of chip balance. You may wish to create/define additional functions (which are called from within this function) to assist with this task. • Good programming practice: o Consistent commenting, layout and indentation. You are to provide comments to describe: your details, program description, all variable definitions, all function definitions and every significant section of code. o Meaningful variable names. 11 of 33 Your solutions MAY make use of the following: • Built-in functions int(), input(), print(), range(), open(), close(), len(), format() and str(). • Concatenation (+) operator to create/build new strings. • The list_name.append(item) method to update/create lists. • Access the individual elements in a string with an index (one element only). i.e. string_name[index]. • Access the individual elements in a list with an index (one element only). i.e. list_name[index] or list_name[i][j]. Your solutions MUST NOT use: • Built-in functions (other than the int(), input(), print(), range(), open(), close() len(), format() and str() functions). • Slice expressions to select a range of elements from a string or list. i.e. name[start:end]. • String or list methods (i.e. other than those mentioned in the 'MAY make use' of section above and those needed for the read_file() function). • Global variables as described in week 10 material. • The enumerate() function. • List comprehensions. • Dictionary to store data items. • Do not use break, or continue statements in your solution – doing so will result in a significant mark deduction. Do not use the return statement as a way to break out of loops. Do not use quit() or exit() functions as a way to break out of loops. PLEASE NOTE: You are reminded that you should ensure that all input and output conform to the assignment specifications. If you are not sure about these details you should check with the sample output provided at the end of this document or post a message to the discussion forum in order to seek clarification. Please ensure that you use Python 3.12.2 or a later version (i.e. the latest version) in order to complete your assignments. Your programs MUST run using Python 3.12.2 (or latest version). 12 of 33 STAGES It is recommended that you develop this part of the assignment in the suggested stages. Many problems in later stages are due to errors in early stages. Make sure you have finished and thoroughly tested each stage before continuing. The following stages of development are recommended: Stage 1 To begin, download the provided files (available on the course website – Assessment Tab) and re-name assign2_yourEmailId.py to assign2 _ yourEmailId.py. i.e. assign2_bonjy007.py Define an empty list to store the player information. For example: player_list = [] Call function play_one_game() (provided for you in the blackjack.py module). Note: you may simply download it from the course website under the Assessment tab. The file blackjack.py should be placed in the same directory as your assign2_yourEmailId.py file. Please do NOT modify the blackjack.py module. Import the blackjack module and call function play_one_game() as seen below. You may wish to read the material on modules posted on the course website (under the assessment tab). import blackjack player_list = [] no_chips = 100 game_result = 0 # Plays one game of blackJack and assigns the result of the game and the chip balance # to variables game_result and no_chips respectively. game_result, no_chips = blackjack.play_one_game(no_chips) # Display to the screen the result of play_one_game() –game result (value of 3, 1 or 0) # and number of chips remaining. print(game_result, no_chips) Make sure the program runs correctly. Once you have that working, back up your program. Note: When developing software, you should always have fixed points in your development where you know your software is bug free and runs correctly. Once you are sure that your program is working correctly, you can either remove or comment out the above statements – we just wanted to make sure it was working correctly! You can bring it back later (appropriately positioned). Stage 2 Write the code for functions read_file() and display _ players() as specified above. Add code to call these two functions to ensure they are working correctly. You may write your own read_file() function or you may use the one provided for you (included in the provided file). 13 of 33 Stage 3 Now that you know the information is being correctly stored in your players list, write the code for function write_to_file(). Add code to call this function to ensure it is working correctly. Stage 4 Implement the interactive mode, i.e. to prompt for and read menu commands. Set up a loop to obtain and process commands. Test to ensure that this is working correctly before moving onto the next stage. You do not need to call any functions at this point, you may simply display an appropriate message to the screen, for example: Sample output: Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: display Not a valid command - please try again. Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: list In list command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: buy In buy command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: search In search command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: high In high command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: add In add command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: remove In remove command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: play In play command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: chips In chips command Please enter choice [list, buy, search, high, add, remove, play, chips, quit]: quit Menu input should be validated with an appropriate message being displayed if incorrect input is entered by the user. 14 of 33 Stage 5 Implement one command at a time. Test to ensure the command is working correctly before starting the next command. Start with the quit and list commands as they do not need you to add anything further to the file other than ensuring that the function calls are in the correct place. You should be able to see that for most commands there is a corresponding function(s). For the remaining commands, the following implementation order is suggested (note: this is a guide only): • list command (display_players() function). • search command (find_player() function). • add command (add_player() function). • buy command (buy_player_chips() function). • remove command (remove_player() function). • high command (display_highest_chip_holder() function). • play command (play_blackjack_games() function). • chips command (sort_by_chips() function). Stage 6 Ensure that you have validated all user input with an appropriate message being displayed for incorrect input entered by the user. Add code to validate all user input. Hint: use a while loop to validate input. Stage 7 Finally, check the sample output (see section titled ‘Sample Output’ towards the end of this document) and if necessary, modify your code so that: • The output produced by your program EXACTLY adheres to the sample output provided. • Your program behaves as described in these specs and the sample output provided. ```Python