I'm a beginner in Python, and I'm stuck on a task I need to complete.
I need to define a function that returns a list in a given range in which every time the number 7 appears or the number % 7 == 0 (no remainder), it is replaced with the word 'BOOM', otherwise the number returns the same. It needs to be done using the 'for' loop.
It’s supposed to look like this:
print(seven_boom(17))
['BOOM', 1, 2, 3, 4, 5, 6, 'BOOM', 8, 9, 10, 11, 12, 13, 'BOOM', 15, 16, 'BOOM']
print(seven_boom(17))
This is what I’ve tried, and I don’t have any idea what to do (end_number is the end of the range in the list, but it needs to be included):
def seven_boom(end_number):
list = [range(0, end_number + 1)]
for i in list:
if i % 7 == 0 or i[0:] == 7:
i += 1
return list.replace(7, 'BOOM')
listSecond, to create a list from a range object uselist(range(...))i[0:] == 7is alwaysFalse:i[0:]is a list.