he 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 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 c
- 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 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
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.")
Trending now
This is a popular solution!
Step by step
Solved in 2 steps