-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)
6
  • 2
    elif and else should be indented at the same level as if Commented Nov 4, 2016 at 12:00
  • You have indentation problems. You ought to use a modern editor like PyCarm to avoid this. Commented Nov 4, 2016 at 12:02
  • 1
    If you are new, the first step is to study, not ask for answers. Commented Nov 4, 2016 at 12:03
  • here's a working version: ideone.com/BdXbaL Commented Nov 4, 2016 at 12:04
  • @w0lf Thanks! it worked. Commented Nov 4, 2016 at 12:04

3 Answers 3

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

Comments

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)

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.