1

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')
5
  • 3
    First, of all, don't name your variables with names that are already used by the language, like list Second, to create a list from a range object use list(range(...)) Commented Jul 13, 2022 at 12:13
  • i[0:] == 7 is always False: i[0:] is a list. Commented Jul 13, 2022 at 12:14
  • It is great you've shown what you have tried to do. Now explain what your solution is actually doing and why that isn't what you expected it to do. Figure out what parts of it aren't working, then ask specific questions about those parts you're having difficulty with (rather than just an entire homework question). Good luck! Commented Jul 13, 2022 at 12:25
  • Related: Seven (7) boom game in one line code in Python Commented Nov 22 at 15:51
  • Related: Python 7BOOM problem: Why is it not working? Commented Nov 22 at 16:06

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

You might want to change a condition since it doesnt take into account numbers with a 7 in it others than 7 (like 17 in the example output). For example your first condition could be if '7' in str(x) instead of x==7
Yup, you're right, updated with that
1

I think this can answer your question.

listEx = [x for x in range (1,20)] ## Creates a list using ints from 1 to 19


def changeList(listex): # Get a list as a parameter
text = 'boom' # You can change the text its the element to implement if condition meets
listnew = [item if item % 7 !=  0 and '7' not in str(item) else text for item in listex ] # Checks if number is divisible by 7 and changes the appending element
return listnew # Return the changed list

It can be done simpler and cleaner, but for now it should be enough. Also check list comprehensions in Python: List Comprehension.

3 Comments

Your answer doesnt take into account when the number itself contains a '7'. Like for 17 in the example output.
wow sorry i kinda missed that part i will change the code in a sec.
Revision 2 made it not compile: "SyntaxError: 'return' outside function"
-1

I'm sure someone will edit it but next time use the curly brackets to make code easy to read. Also don't use "list" as a variable name as it is a keyword in Python. List will convert your data into a list form. I don't think leaving range in the square brackets is a good idea.

word = []
def seven_boom(end_number):
    numbers = list(range(0, end_number + 1))

    for i in numbers:
        if i % 7 ==0 or '7' in str(i):
            word.append("BOOM")
        else:
            word.append(i)
    return word

1 Comment

This needs return word statement at the button (watch your indents as well). Also, as Titouan L has pointed out, needs to account for if the number 7 appears in the given number. So replacing your if statement with if i % 7 ==0 or "7" in str(i): would work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.