You are required to write a Python program that will manage character (heroes and villain) information. Character
Fantastic news! We've Found the answer you've been seeking!
Question:
Write a menu driven program called uopsp_assignment3_EmailId.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 character information (using a List of Lists). Character information will be stored in a text file that will be read in when the program commences. Once the initial character data has been read in from the file, the program should allow the user to interactively query and manipulate the character information.
Input
When your program begins, it will read in character (hero and villain) information from a file called characters.txt. This is a text file that stores information pertaining to characters (heroes and villains). An example input file called characters.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 character (hero or villain) is stored on a separate line. The very next line contains the secret identity of the character (hero or villain). The very next line contains a character specifying whether the character is a hero ('h') or villain ('v'), followed by the number of battles fought, battles won, battles lost, battles drawn, and a health value. 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.
Wonder Woman
Diana Prince
h 5 5 0 0 90
Batman
Bruce Wayne
h 6 2 0 4 80
The Joker
Jack Napier
v 5 1 0 4 80
Superman
Clark Kent
h 7 4 0 3 100
Catwoman
Selina Kyle
v 12 0 6 6 50
Aquaman
Arthur Curry
h 8 2 2 4 30
Iron Man
Tony Stark
h 10 8 2 0 50
Hulk
Bruce Banner
h 7 2 1 4 80
Thanos
n/a
v 10 2 0 8 90
Your program will maintain one List of Lists as follows:
character_list = [] # List of Lists to store character information
Once the above information is read in from the file, the character list will be populated as follows:
character_list
[Wonder Woman, Diana Prince, h, 5, 5, 0, 0, 90]
[Batman, Bruce Wayne, h, 6, 2, 0, 4, 80]
[The Joker, Jack Napier, v, 5, 1, 0, 4, 80]
[Superman, Clark Kent, h, 7, 4, 0, 3, 100]
[Catwoman, Selina Kyle, v, 12, 0, 6, 6, 50]
[Aquaman, Arthur Curry, h, 8, 2, 2, 4, 30]
[Iron Man, Tony Stark, h, 10, 8, 2, 0, 50]
[Hulk, Bruce Banner, h, 7, 2, 1, 4, 80]
[Thanos, n/a, v, 10, 2, 0, 8, 90]
Note: A character and their statistics are stored as a list and are stored as one item in the character list, i.e. a List of Lists (as seen above).
Interactive Mode
Your program should enter an interactive mode after the character (hero and villain) 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:
list:
Displays for all characters, the character's name, number of battles, battles, won, lost, drawn, and their health value. Outputs the contents of the character list (heroes and villains) 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'.
heroes:
Displays for all heroes, the character's name, number of battles, battles, won, lost, drawn, and their health value. Outputs the contents of the character list (heroes and villains) 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'.
villains:
Displays for all villains, the character's name, number of battles, battles, won, lost, drawn, and their health value. Outputs the contents of the character list (heroes and villains) 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'.
search:
Prompts for and reads the character's (hero/villain's) name and searches for the character in the character (hero and villain) list. If the character is found in the character list, the character's name, secret identity, battles fought, won, lost, drawn, and their health value, are displayed to the screen as seen below (in the section titled Screen Format). If the character is not found in the list of characters (heroes and villains), an error message stating the character has not been found is displayed to the screen.
reset:
Prompts for and reads the character's (hero/villain's) name and searches for the character in the character list (heroes and villains). If the character is found in the list of characters (heroes and villains), the character's health value is reset to 100. If the character is not found in the list of characters (heroes and villains), an error message stating the character has not been found is displayed to the screen.
add:
Prompts for and reads the character's (hero or villain's) name, secret identity and whether the character is a hero or villain. If the character does not already exist (i.e. a match is not found on the character's name), the character is added to the list of characters (heroes and villains). If the character is added, the health value is initialised to 100 and all other stats (number battles, no won, no lost, no drawn), are initialised to zero and a message is displayed to the screen indicating that this has been successfully added.
The character information must be added after the last character entry stored in the list (i.e. at the end of the list). If the character is already stored in the character list, an error message is displayed. No duplicate entries are allowed.
remove:
Prompts for the character's name. If the character is found, he/she is removed from the list of characters (heroes and villains) and a message is displayed to the screen indicating that this has been done. If the character is not found in the character list, an error message is displayed.
high:
Displays the character with the highest number of battles won in the list of characters. Where two characters have the same number of battles won, the character with the lower number of battles fought should be displayed to the screen - see section titled 'Screen Format' below. If no characters are stored in the list or a character with the highest number of battles won cannot be found (i.e. all characters have zero battles won), display an error message accordingly.
battle:
Prompts for the name of the two opponents to do battle. The program searches for each character in the list of characters (heroes and villains) and if the character is not found in the list of characters, an error message is displayed to the screen and the user is asked to enter another character (hero/villain).
If the opponents are found in the list of characters (heroes and villains), they are then able to do battle and the number of battle rounds the heroes/villains will undertake (a number between 1-5 inclusive) is prompted for and read. One battle may have many (1-5 inclusive) rounds. The heroes/villains battle until either an opponent dies (health status is reduced to zero) or the number of battle rounds have been completed. For each individual battle round, the hero/villain will sustain a certain amount of damage to their health rating. The amount of damage sustained from the battle will be a randomly generated value between 0-50 inclusive. After each round, each opponents damage value (i.e. randomly generated number between 0-50 inclusive) and current health value (i.e. calculated by: health value - damage value) are displayed to the screen.
After every battle (however many rounds), a winner is determined (i.e. the opponent with the higher health value wins the battle) and the opponents' battle statistics are updated (i.e. number of battles, no won, no lost, etc...) accordingly.
health:
Displays the list of characters (heroes and villains) in descending order of health. Where two characters have the same health status, the character with the highest number of battles fought should appear first. This command should not alter the original list of characters in any way. The information is displayed to the screen as described in the section titled 'Screen Format' below.
quit:
Causes the program to quit, outputting the contents of the character list (List of lists) to a file (see section 'Final Output' below for format).
Note:
The program should display an appropriate message if a character is not found matching a search criteria. Appropriate messages should also be displayed to indicate whether a command has been successfully completed.
Two characters may have the same secret identity, e.g., if both of them don't have secret identities, then use n/a.
Character names are case sensitive.
If no characters are stored in the list or a character with the highest number of battles won cannot be found or where two characters have the same number of battles won and same number of battles fought, display the message "No such character found; please try again later."
A character can NOT battle against himself/herself.
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, heroes, villains, search, reset, add, remove, high, battle, health, quit]:
Menu input should be validated with an appropriate message being displayed if incorrect input is entered.
Screen Format
The list, heroes and villains commands (display_characters() function) should display the character (hero and villain) information in the following format:
===================================================
- Character (heroes and villains) Summary -
===================================================
- P W L D Health -
---------------------------------------------------
- Wonder Woman 5 5 0 0 90 -
---------------------------------------------------
- Batman 6 2 0 4 80 -
---------------------------------------------------
- The Joker 5 1 0 4 80 -
---------------------------------------------------
- Superman 7 4 0 3 100 -
---------------------------------------------------
- Catwoman 12 0 6 6 50 -
---------------------------------------------------
- Aquaman 8 2 2 4 30 -
---------------------------------------------------
- Iron Man 10 6 3 1 50 -
---------------------------------------------------
- Hulk 7 2 1 4 80 -
---------------------------------------------------
- Thanos 10 2 0 8 90 -
---------------------------------------------------
===================================================
Likewise for the health command (which should use the same display_characters() function) should display the character (hero and villain) information in the following format:
===================================================
- Character (heroes and villains) Summary -
===================================================
- P W L D Health -
---------------------------------------------------
- Superman 7 4 0 3 100 -
---------------------------------------------------
- Thanos 10 2 0 8 90 -
---------------------------------------------------
- Wonder Woman 5 5 0 0 90 -
---------------------------------------------------
- Hulk 7 2 1 4 80 -
---------------------------------------------------
- Batman 6 2 0 4 80 -
---------------------------------------------------
- The Joker 5 1 0 4 80 -
---------------------------------------------------
- Catwoman 12 0 6 6 50 -
---------------------------------------------------
- Iron Man 10 6 3 1 50 -
---------------------------------------------------
- Aquaman 8 2 2 4 30 -
---------------------------------------------------
===================================================
The search command should display individual character (hero/villain) information to the screen in the following format:
All about Catwoman --> VILLAIN
Secret identity: Selina Kyle
Battles fought: 12
> No won: 0
> No lost: 6
> No drawn: 6
Current health: 50%
Or if a hero...
All about Aquaman --> HERO
Secret identity: Arthur Curry
Battles fought: 8
> No won: 2
> No lost: 2
> No drawn: 4
Current health: 30%
The high command (display_highest_battles_won() function) should display the character with the highest number of battles won in the following format:
Highest number of battles won => Iron Man with 6 opponents defeated!
Final Output
When your program finishes (because you entered the quit command) your program should output the contents of the list of characters to a file called new_characters.txt.
The format of this file should exactly match that of the input file.
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 uopsp_assignment3_EmailId.py.
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).
You must implement each function listed below.
Appropriate if, if-else, if-elif-else statements (as necessary).
The following functions:
read_file(filename)
This function takes a file name and reads the contents of that file into a list called character_list. The function returns the newly created list of characters (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.
write_to_file(filename, character_list)
This function will output the contents of the character 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 character objects. You must use a loop in your solution.
display_characters(character_list, display_type)
This function will take the list of characters and a display_type as a parameter and will output the contents of the list to the screen. If the display type is 0, all characters will be displayed to the screen. If the display type is 1, only heroes will be displayed to the screen. If the display type is 2, only villains will be displayed 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'.
find_character(character_list, name)
This function will take the character's name as input along with the list of characters (character_list) and will return the position (index) of the character found in the character_list. If the character is not found, the function returns -1. You must use a loop in your solution. You must not use list methods in your solution.
add_character(character_list, name, secret_identity, hero)
This function takes the list of characters and the character's (to be added) name, secret_identity and hero ('h' | 'v') as parameters. If the character already exists in the list of characters, display an error message to the screen. If the character does not exist in the characters (heroes and villains) list, the character is added. Create a new list with the information read in (i.e. name, secret_identity and hero) and add the character's information (as a list) to the end of the list of characters. If the character is added, health is initialised to 100 and all other stats data (number of battles, no won, no lost, no drawn), are initialised to zero and a message is displayed to the screen indicating this has been successfully added. This function returns the list of characters. You may use the list_name.append(item) method to add the new character to the list of characters. You must call function find_character() from this function.
remove_character(character_list, name)
This function takes the list of characters and the character's (to be removed) name as parameters. If the character is not found in the list of characters, display an error message to the screen. If the character does exist in the characters list, this function removes the character and displays a message to the screen indicating that the character has been removed from the characters list. This function returns the list of characters. You may use the list_name.append(item) method in this function. You must call function find_character() from this function.
display_highest_battles_won(character_list)
do_battle(character_list, opponent1_pos, opponent2_pos)
This function takes the character list as a parameter and displays the character with the highest number of battles won in the list of characters to the screen. Where two characters have the same number of battles won, the character with the lower number of battles fought should be displayed to the screen. If no characters are stored in the list or a character with the highest number of battles won cannot be found (i.e. all characters have zero battles won), display an error message accordingly.
This function takes the list of characters and the position of the two characters that are about to do battle (i.e. position is the location/index of the character stored in the list of characters. This is useful so we can update the character's health value and battle statistics after every battle).
This function prompts for and reads the number of battle rounds the heroes/villains will undertake (a number between 1-5 inclusive). The function allows the heroes/villains to battle until either an opponent dies (health status is reduced to zero or less) or the number of battle rounds have been completed. For each individual battle round, the hero/villain will sustain a certain amount of damage to their health rating. The amount of damage sustained from the battle will be a randomly generated value between (0 - 50 inclusive).
General algorithm is as follows:
while (number of battle rounds have not been completed and both opponents are still alive)
randomly generate a damage value sustained from battle and update opponent 1's health value by
calling method (your own defined one), e.g., update_health(character_list, damage1) or implementing proper operations.
randomly generate a damage value sustained from battle and update opponent 2's health value by
calling method (your own defined one), e.g., update_health(character_list, damage2) or implementing proper operations.
display opponent 1 round results (as seen in the sample output)
display opponent 2 round results (as seen in the sample output)
determine the winner of the battles - the character with the most health left at the end of all the battle rounds is the winner.
display the winner to the screen and also report if a character has died as a result of battle (refer to sample output for layout).
If a battle results in a tie, print the message "-- A tie. Nobody wins."
If both characters die in a battle, print the character "has died" messages in two lines.
update opponent 1's battle statistics. Hint: Increment the number of battles by one, number won, lost, drawn accordingly. We increment battles by one because battles may consist of a number of battle rounds.
update opponent 2's battle statistics. Hint: Increment the number of battles by one, number won, lost, drawn accordingly. We increment battles by one because battles may consist of a number of battle rounds.
sort_by_health (character_list)
This function takes the list of characters and returns a copy of the list in descending order of health. Where two characters have the same health status, the character with the higher number of battles fought should appear first. This function must NOT modify the character 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 character list sorted in descending order of health. You may wish to create/define additional functions (which are called from within this function) to assist with this task.
Related Book For
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch
Posted Date: