-4

Python says it has a syntax error in this line " elif (i%7==0) or str.count(str(i),'7')>0: " and I cannot figure it out. I'm new to Python so it must be something simple.

k=int(input("enter the value for k:"))

n=int(input("enter the value for n:"))
if k>=1 and k<=9:
    for i in range(1,n+1):

          if (i%7==0) and str.count(str(i),'7')>0:
              print("boom-boom!")
            elif (i%7==0) or str.count(str(i),'7')>0:
                  print("boom")
                else: print(i)
Sam12
  • 1,805
  • 2
  • 15
  • 23

3 Answers3

1

The issue is with your identation:

Make sure the "elif" is inline with your "if" and also your "else" statement. Python is sensitive to indentations and spaces.

if (i%7==0) and str.count(str(i),'7')>0:
    print("boom-boom!")
elif (i%7==0) or str.count(str(i),'7')>0:
    print("boom")
else: 
    print(i)
Tolulope Owolabi
  • 314
  • 3
  • 13
0

Add proper indentation:

k=int(input("enter the value for k:"))

n=int(input("enter the value for n:"))
if k>=1 and k<=9:
    for i in range(1,n+1):
        if (i%7==0) and str.count(str(i),'7')>0:
            print("boom-boom!")
        elif (i%7==0) or str.count(str(i),'7')>0:
            print("boom")
        else: 
            print(i)
Nurjan
  • 5,889
  • 5
  • 34
  • 54
0

Here is an improved solution:

k = int(input("enter the value for k:"))
n = int(input("enter the value for n:"))

if 1 <= k <= 9:
    for i in range(1, n + 1):
        text = str(i)
        if i % 7 == 0 and text.count('7'):
            print("boom-boom!")
        elif i % 7 == 0 or text.count('7'):
            print("boom")
        else:
            print(i)
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103