CSE 231 Fall 2022 Computer Project #06
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CSE 231
Fall 2022
Computer Project #06
This assignment focuses on the design, implementation, and testing of a Python program that uses lists and tuples.
It is worth 50 points (5% of course grade) and must be completed no later than 11:59 PM on
Sunday 10/23, 2022.
Note that there will be zero penalties for assignments handed in on the following two days: October 24 & 25. No submissions will be accepted after October 25. If you submit your project by Sunday 10/23 at 11:59PM EST, you will get 2 extra credit points (note that you have to click on the submit to count as submitted).
Assignment Overview
In this assignment, you will practice with lists of lists and tuples and write a program to answer the questions described below.
Assignment Background
In this assignment, you will practice with character data from a video game called Genshin Impact. Genshin Impact has various playable characters that can be chosen. Each character has five attributes: its name, an element, a weapon type, a rarity ranking, and a possible region that they originate from.
The data is provided in a file:
1) data .csv: Each line has the five attributes of a character, comma-separated Name , Rarity , Element , Weapon , Region
You will create a list of tuples in a slightly different order:
(name, element, weapon, rarity, region)
Assignment Specifications
You will develop a Python program that has the following functions
open_file () > file pointer fp :
a. This function prompts the user to input a file name to open and keeps prompting until a valid name is entered. Return the file pointer.
b. Parameters: none
c. Returns: a file pointer
d. Display: prompts and error messages
read_file (fp) > list of tuples :
a. This function reads the comma-separated value (csv) file using file pointer fp. The file has one header line. Create a list of tuples. Each tuple represents a character and has the following format:
(name, element, weapon, rarity, region)
The type of each element in the tuple:
(string, string, string, int, string)
If there is no value for the region, use None.
Return the list of tuples.
b. Parameter: file pointer
c. Returns: list of tuples
d. Displays: nothing
def get_characters_by_criterion (list of tuples, criteria, value)
> list
a. Given a list of character tuples, retrieve the characters that match a certain criteria. If there is a problem with a value or criteria parameter, don’t add the character to the return list—see note below
The criteria parameter is an int that represents the index of a criteria in a character tuple. For example, if filtering by weapon, criteria should be 2; if filtering by region, criteria should be 4, etc (Hint: use provided constants for the parameter criteria).
Note:
I. The input list of tuples can be empty. In that case, return an empty list.
II. If the criteria is RARITY, value must be an int . If not, the character is not added to the return list. Remember, criteria is an INT.
III. For other criteria, value must be a string and variations in case are accepted, e.g. 'Sword ', 'SWORD ', 'SwOrD ' and similar will match. If not, do not add the character to the return list.
IV. If a character’s criterion value is None, do not add the character to the return list.
b. parameter: list of tuples, int, int/string
c. Returns: list of tuples
d. Display: nothing
def get_characters_by_criteria (master_list, element, weapon, rarity)> list :
This function takes as parameter the list of tuples returned by the read_file function (master_list), an element, a weapon, and a rarity and returns a list of tuples filtered using those 3 criterias. This function looks similar to the get_characters_by_criterion function; the difference is that this function calls get_characters_by_criterion three times, one for each of element, weapon and rarity specified to select characters based on all three criteria. The three calls effectively filter for each by passing the returned list of one function as an argument to the next call. Note that the parameters in this function for element, weapon, and rarity become the
value arguments for the three respective calls to get_characters_by_criterion---the criteria argument is determined by this parameter’s name. (Hint: use provided constants for the parameter criteria).
a. Parameter: list of tuples, string, string, int
b. Returns: list of tuples
c. Display: nothing
def get_region_list (master_list) > list :
a. Given the master list of character tuples, retrieve all available regions into a list. No duplicate is allowed. If the region is None, do not include it in the list. Sort the list alphabetically (for consistent testing). Hint: to prevent duplicates check if a region is not in the list before adding it to the list.
b. Parameters : list of tuples
c. Returns: sorted list of strings
d. Display: nothing
def sort_characters (list_of_tuples) > list :
a. Given a list of character tuples, create a new list where character tuples have been sorted. The order of sorting is by decreasing rarity and alphabetically by name. Because sorting is stable, sort alphabetically on names, and then sort again by rarity. By default, sorting sorts on the first index—names in this case. To then sort by rarity at index 3 use itemgetter as described in the Notes below. Remember when sorting rarity to reverse the sorting so it is decreasing. Do not modify the original list.
b. Parameters: list of tuples
c. Return : sorted list of tuples
d. Display: nothing
def display_characters (list_of_tuples) > none :
a. Given a list of character tuples, display the characters along with their information, using the formats (HEADER and ROWS) provided as constants in prog06.py. If a region has the value None, display 'N/A'.
If list_of_tuples is empty, print 'Nothing to print. '
For example, if the input is the following:
list_of_tuples = [('Aloy', 'Cryo', 'Bow', 5, None), ('Eula',
'Cryo', 'Claymore', 5, 'Mondstadt')]
Below is what is displayed:
Character Element Weapon Rarity Region
Aloy Cryo Bow 5 N/A
Eula Cryo Claymore 5 Mondstadt
b. Parameters: list of tuples
c. Return: nothing
d. Display: character attributes
def get_option () > int :
a. Display a menu of options and prompt for input (MENU in the starter code). Note that the prompt is part of the MENU. Ask for the input in the function, this function does not take parameters. If the user enters a valid option (the input is an integer between 1 and 4), return the integer, otherwise print an error message ( INVALID_INPUT in the starter code).
b. Parameters: nothing
c. Return: int
d. Display: menu and error message
main():
1. Call open_file() to open a CSV file for reading and get a file pointer for the input file.
2. Call read_file() to read the data and store it in a list of lists.
3. Call get_option() to prompt the user for input.
4. Loop until the user chooses 4.
5. Based on the input option
a. If option is 1, call a function to get all available regions
i. Display the message "\nRegions:"
ii. Display all regions, separated by ", " (comma and a space)(Hint: two possibilities: use the join() method or build a string with a comma tacked onto each region and at the end strip off the trailing comma before printing)
b. If option is 2
i. Prompt first for a criteria (use CRITERIA_INPUT in your prompt). if criteria is not an int between 1 and 4 (inclusive), print an error message (use INVALID_INPUT) and re-prompt for a criteria. Otherwise, prompt for a value (use VALUE_INPUT in your prompt).
if criteria is RARITY, convert value to an int . If it is not an int, print an error message (use INVALID_INPUT) and re-prompt.
ii. Call get_characters_by_criterion to filter characters by a certain criteria with a value.
iii. Sort characters using sort_characters
iv. Display characters using display_characters
c. If option is 3
i. Prompt in this order for element, weapon, rarity; (use ELEMENT_INPUT,
WEAPON_INPUT, RARITY_INPUT respectively) for RARITY convert value to an int; print an error message (use INVALID_INPUT) and re-prompt if it is not an int.
ii. Call get_characters_by_criteria to filter characters by above criteria.
iii. Sort characters using sort_characters
iv. Display characters using display_characters
d. If option is 4
i. Quit the program
Assignment Notes and Hints
1. The coding standard for CSE 231 is posted on the course website:
http://www.cse.msu.edu/~cse231/General/coding.standard.html
Items 1-9 of the Coding Standard will be enforced for this project.
2. By default, sorted (or sort) will sort on the first item in a list or tuple. To sort on other indices use itemgetter . First, remember to include the following at the top of your program: from operator import itemgetter
Then you use the key argument in sorted (or sort). The following sorts list L on index 3:
L = sorted(L, key=itemgetter(3))
3. The program will produce reasonable and readable output, with appropriate labels for all values displayed.
4. We provide a proj06.py program for you to start with.
5. If you “hard code” answers, you will receive a grade of zero for the whole project. An example of hard coding is to simply print the approximate value of e rather than calculating it and then
printing the calculated average.
Assignment Deliverable
The deliverable for this assignment is the following file:
proj06.py – the source code for your Python program
Be sure to use the specified file name and to submit it for grading before the project deadline.
Test 1
Enter file name: data .csv
Welcome to Genshin Impact Character Directory
Choose one of below options:
1. Get all available regions
2. Filter characters by a certain criteria
3. Filter characters by element, weapon, and rarity
4. Quit the program
Enter option: 4
Test 2
Enter file name: data .csv
Welcome to Genshin Impact Character Directory
Choose one of below options:
1. Get all available regions
2. Filter characters by a certain criteria
3. Filter characters by element, weapon, and rarity
4. Quit the program
Enter option: 1
Regions:
Inazuma, Liyue, Mondstadt, Snezhnaya
Welcome to Genshin Impact Character Directory
Choose one of below options:
1. Get all available regions
2. Filter characters by a certain criteria
3. Filter characters by element, weapon, and rarity
4. Quit the program
Enter option: 4
Test 3
Enter file name: data .csv
Welcome to Genshin Impact Character Directory
Choose one of below options:
1. Get all available regions
2. Filter characters by a certain criteria
3. Filter characters by element, weapon, and rarity
4. Quit the program
Enter option: 2
Choose the following criteria
1. Element
2. Weapon
3. Rarity
4. Region
Enter criteria number: 1
Enter value: time
Nothing to print.
Welcome to Genshin Impact Character Directory
Choose one of below options:
1. Get all available regions
2. Filter characters by a certain criteria
3. Filter characters by element, weapon, and rarity
4. Quit the program
Enter option: 2
Choose the following criteria
1. Element
2. Weapon
3. Rarity
4. Region
Enter criteria number: 1
Enter value: cryo
Character |
Element |
Weapon |
Rarity |
Region |
Aloy |
Cryo |
Bow |
5 |
N/A |
Eula |
Cryo |
Claymore |
5 |
Mondstadt |
Ganyu |
Cryo |
Bow |
5 |
Liyue |
Kamisato Ayaka |
Cryo |
Sword |
5 |
Inazuma |
Qiqi |
Cryo |
Sword |
5 |
Liyue |
Shenhe |
Cryo |
Polearm |
5 |
Liyue |
Chongyun |
Cryo |
Claymore |
4 |
Liyue |
Diona |
Cryo |
Bow |
4 |
Mondstadt |
Kaeya |
Cryo |
Sword |
4 |
Mondstadt |
Rosaria |
Cryo |
Polearm |
4 |
Mondstadt |
Welcome to Genshin Impact Character Directory
Choose one of below options:
1. Get all available regions
2. Filter characters by a certain criteria
3. Filter characters by element, weapon, and rarity
4. Quit the program
Enter option: 2
Choose the following criteria
1. Element
2. Weapon
3. Rarity
4. Region
Enter criteria number: 1
Enter value: none
Character Element Weapon Rarity Region
Traveler None Sword 5 N/A
Welcome to Genshin Impact Character Directory
Choose one of below options:
1. Get all available regions
2. Filter characters by a certain criteria
3. Filter characters by element, weapon, and rarity
4. Quit the program
Enter option: 4
Test 4
Enter file name: data .csv
Welcome to Genshin Impact Character Directory
Choose one of below options:
1. Get all available regions
2. Filter characters by a certain criteria
3. Filter characters by element, weapon, and rarity
4. Quit the program
Enter option: 2
Choose the following criteria
1. Element
2. Weapon
3. Rarity
4. Region
Enter criteria number: 2
Enter value: polearm
2022-10-26