简单语法E

2024-05-19 01:44:52 发布

您现在位置:Python中文网/ 问答频道 /正文

Python说它在“elif(i%7==0)或str.计数(str(i),'7')>;0:“我想不出来。 我是Python新手,所以它一定很简单。你知道吗

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)

Tags: andthegtforinputifvaluecount
3条回答

添加适当的缩进:

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)

问题在于你的身份:

确保“elif”与“if”和“else”语句一致。 Python对缩进和空格很敏感。你知道吗

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)

下面是一个改进的解决方案:

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)

相关问题 更多 >

    热门问题