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')
Something like this should work:
end_number = 18
def seven_boom(end_number):
List = range(0, end_number + 1)
boomList = ["BOOM" if (x%7 == 0) or ("7" in str(x)) else x for x in List ]
return boomList