此代码在第一个for循环上显示“无效语法”错误

问题描述 投票:-1回答:1
n=int(input())
ls=[]
ls2=[]
ls=list(map(int,input().split())
for i in range (n): # here the compiler is showing invalid syntax
    if ls[i]==0:
        ls2.append(i)
for j in range(n-2):
    if ls2[j]+2 in ls2:
        ls3.append(ls[j]+2)
print(ls3)
python for-loop syntax
1个回答
0
投票

您在第四行上缺少括号

n=int(input())
ls=[]
ls2=[]
ls=list(map(int,input().split()))
#                               ^
#You were missing a paranthese
for i in range (n): # here the compiler is showing invalid syntax
    if ls[i]==0:
        ls2.append(i)
for j in range(n-2):
    if ls2[j]+2 in ls2:
        ls3.append(ls[j]+2)
print(ls3)
© www.soinside.com 2019 - 2024. All rights reserved.