Question: I am working on a coding assignment in Python and I am struggling to understand why my code is not working. Write a function in
I am working on a coding assignment in Python and I am struggling to understand why my code is not working.
Write a function in Python called "BooHoo" which takes in an integer, n, and stores all integers from 1 to n in a list. However, if an integer in the list is divisible by 3, the list should instead contain "Boo". If the integer is divisible by 5 the list should instead contain "Hoo". If the integer is divisible by both 3 and 5, the list should instead contain "BooHoo".
def BooHoo(n): ''' A function which returns a list of integers, and replaces integers divisible by 3 with "Boo" and integers divisible by 5 with "Hoo" and integers divisible by 3 and 5 with "BooHoo" Parameters ------- n: an integer Returns --------
final_list: a Python list ''' main_list = [] for x in range(n): main_list.append(x) # Replace every fourth item with buzz for idx, j in enumerate(main_list): if not (idx % 3) and not (idx % 5): main_list.pop(idx) main_list.insert(idx, 'BooHoo') elif not (idx % 3): main_list.pop(idx) main_list.insert(idx, 'Boo') elif not (idx % 5): main_list.pop(idx) main_list.insert(idx, 'Hoo') else: continue final_list = [main_list] return final_list
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
