当我运行我的程序找到数字的反向排列时,它返回我的跟随错误:
python inverse_permutation.py 1 2 3 4 5
File "inverse_permutation.py", line 18
else:
^
SyntaxError: invalid syntax
在提出这个问题之前,我曾尝试查看类似的帖子,但无法找到解决我特定问题的方法。这是我的代码:
import stdarray
import stdio
import sys
q = input().split()
for i in range(len(q)):
q[i] = int(q[i])
l = len(q)
s = int((l*(l+1))/2-(l))
s2 = 0
b = [0 for i in range(l)]
for i in range(l):
s2 = s2+q[i]
if(s == s2):
for i in range(l):
b[q[i]] = i
print(b)
else:
print('Not a permutation')
答案 0 :(得分:0)
如果应该跟着else,但你把print语句放在if和else之间,你可以做这样的事情
import stdarray
import stdio
import sys
q = input().split()
for i in range(len(q)):
q[i] = int(q[i])
l = len(q)
s = int((l*(l+1))/2-(l))
s2 = 0
b = [0 for i in range(l)]
for i in range(l):
s2 = s2+q[i]
if(s == s2):
for i in range(l):
b[q[i]] = i
else:
print('Not a permutation')
print(b)