Instructions Use the main template provided in the breakout room activity onto your IDE. Add the implementation for the print_categories(), is_valid_index() and update_category() functions Test your...


Instructions



  1. Use the main template provided in the breakout room activity onto your IDE.

  2. Add the implementation for the print_categories(), is_valid_index() and update_category() functions

  3. Test your implementations by running the assert tests in the test file.


Requirements


You will need to implement the following functions.


def print_categories(main_list): """ Given a list of lists, for each list stored in main_list, output its contents as follows: f"{index of category}. {item[0]} - {item[1]}%". note that indexing must start at 1 for the first item, not 0, which is first item's actual index in main_list. If `main_list` is empty, the function prints "There are no categories." Returns the number of categories. """ pass def is_valid_index(idx, in_list): """ Checks whether the provided index `idx` is a valid positive index that can retrieve an element from `in_list`. Returns False if `idx` is negative or exceeds the size of `in_list` - 1. """ pass def update_category(info_str, idx, main_list): """ Given a string with the category information and an integer index of the category that needs to be updated in the list of lists. Call is_valid_index(idx, main_list) to check the validity of the index; returns -3 if `idx` is negative or exceeds the size of `in_list`. Call create_category(info_str) to add the category to the main list; returns the result of calling create_category(), which is a list if the update succeeds or an integer indicating an error. """ pass


In the main program (the grade management system defined in the breakout room lab), add the following lines to prompt the user for the category information when they select to update it:


elif opt == ...: # add the appropriate option key from the menu print_categories(all_categories) print("Which category would you like to update?") print("Enter the number corresponding to the category.") user_option = input() if not user_option.isdigit(): print(f"WARNING: `{user_option}` is an invalid category number!") else: idx = int(user_option) - 1 if not is_valid_index(idx, all_categories): print(f"WARNING: `{user_option}` is an invalid category number!") else: print(f"Updating category {all_categories[idx][0]}") print("Enter the category name and percentage: ") cat_info = input() cat_list = update_category(cat_info, idx, all_categories) if type(cat_list) == list: all_categories[idx] = cat_list else: print("WARNING: invalid category information!") print(f"Category information `{cat_info}` was not added.")


Sample Program Flow


First, let’s check that we correctly detect invalid input:


1. Quizzes - 25.5% 2. Project - 20% Which category would you like to update? Enter the number corresponding to the category. 0 WARNING: `0` is an invalid category number! ************************* 1. Quizzes - 25.5% 2. Project - 20%


Let us update a category by renaming the name and percentage of that category


1. Quizzes - 25.5% 2. Project - 20% Which category would you like to update? Enter the number corresponding to the category. 1 Updating category Quizzes Enter the category name and percentage: Exams 80 ************************* 1. Exams - 80.0% 2. Project - 20%


test print_categories


Sample assert statements for the print_categories function to check that it returns the correct values.


assert print_categories([]) == 0 print() assert print_categories([["Quizzes", 25.5]]) == 1 print() assert print_categories([['Exam1', 20.0], ['Exam2', 25.0]]) == 2


For print_categories, we also need to run it to verify that it is correctly displaying the correct output. For each function call above, we would get the following output:


There are no categories. 1. Quizzes - 25.5% 1. Exam1 - 20.0% 2. Exam2 - 25.0%


test is_valid_index


assert is_valid_index(0, [["Quizzes", 25.5]]) == True assert is_valid_index(1, [["Quizzes", 25.5]]) == False assert is_valid_index(-1, [["Quizzes", 25.5]]) == False assert is_valid_index(1, [["Quizzes", 25.5], ["Project", 20]]) == True


test update_category


Practice adding tests for the function to verify its correctness.


assert update_category("invalid category", -1, []) == -3 assert update_category("invalid category", 1, []) == -3 result = update_category("Reflection 5", 0, [["RA", 2.0]]) expected = ["Reflection", 5.0] print(f'update_category("Reflection 5", 0, [["RA", 2.0]]) \n returned {result} \n expected result is {expected}') assert result == expected


Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here