2016-11-04 3 views
-4

Python sagt, es hat einen Syntaxfehler in dieser Zeile "elif (i% 7 == 0) oder str.count (str (i), '7')> 0:" und ich kann es nicht herausfinden . Ich bin neu in Python, also muss es etwas Einfaches sein.Simple Syntax Error

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) 
+2

'elif' und' else' auf gleicher Höhe eingerückt werden sollte als 'if' – GolfWolf

+0

Sie Einbuchtung Probleme haben. Sie sollten einen modernen Editor wie [PyCarm] (https://www.jetbrains.com/pycharm/) verwenden, um dies zu vermeiden. –

+1

Wenn Sie neu sind, ist der erste Schritt zu studieren, nicht nach Antworten zu fragen. – TigerhawkT3

Antwort

0

richtige Vertiefung hinzufügen:

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) 
1

Das Problem mit Ihrem identation ist:

Stellen Sie sicher, die "Elif" ist inline mit dem "if" und auch Ihre "else" Anweisung. Python reagiert empfindlich auf Einkerbungen und Leerzeichen.

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) 
0

Hier ist eine verbesserte Lösung:

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)