如何返回到python中的第1步函数use while语句
我有一个由两个函数组成的函数:如何返回到python中的第1步函数use while语句,python,Python,我有一个由两个函数组成的函数: def cipher_functions(afile): def swap_joker1(afile): idx = afile.index(27) if idx == len(afile) - 1: afile[idx],afile[0]=afile[0],afile[idx] else: afile[idx],afile[idx+1]=afile[idx
def cipher_functions(afile):
def swap_joker1(afile):
idx = afile.index(27)
if idx == len(afile) - 1:
afile[idx],afile[0]=afile[0],afile[idx]
else:
afile[idx],afile[idx+1]=afile[idx+1],afile[idx]
return afile
def swap_joker2(afile):
idx = afile.index(28)
if idx!=len(afile)-2 and idx!=len(afile)-1:
afile[idx],afile[idx+1],afile[idx+2]=afile[idx+1],afile[idx+2],afile[idx]
elif idx==len(afile)-2 and idx!=len(afile)-1:
a=afile.pop(idx)
afile.insert(1,a)
elif idx!=len(afile)-2 and idx==len(afile)-1:
a=afile.pop(idx)
afile.insert(2,a)
return afile
def swap_jokers(afile):
idx1=afile.index(27)
idx2=afile.index(28)
a=afile[0:idx1]
b=afile[0:idx2]
if len(a) < len(b) and idx1!=0 and idx2!=len(afile)-1:
afile = afile[idx2+1:] + cfile[idx1:idx2+1] + afile[:idx1]
elif len(a)<len(b) and idx1==0 and idx2!=len(afile)-1:
afile = afile[idx2+1:] + afile[idx1:idx2+1]
elif len(a)<len(b) and idx1!=0 and idx2==len(afile)-1:
afile = afile[idx1:idx2+1] + afile[:idx1]
elif len(a)<len(b) and idx1==0 and idx2==len(afile)-1:
afile = afile
elif len(b)<len(a) and idx2!=0 and idx1!=len(afile)-1:
afile = afile[idx1+1:] + afile[idx2:idx1+1] + afile[:idx2]
elif len(b)<len(a) and idx2==0 and idx1!=len(afile)-1:
afile = afile[idx1+1:] + afile[idx2:idx1+1]
elif len(b)<len(a) and idx2!=0 and idx1==len(afile)-1:
afile = afile[idx2:idx1+1] + afile[:idx2]
elif len(b)<len(a) and idx2==0 and idx1==len(afile)-1:
afile=afile
return afile
def bottom_move(afile):
lastvalue=afile[-1]
if lastvalue is 28:
afile=afile
else:
afile=afile[lastvalue:-1]+afile[0:lastvalue]+afile[-1:]
return afile
def top_move(afile):
firstvalue=afile[0]
if firstvalue is 28:
return afile[-1]
else:
return afile[firstvalue]
return top_move(bottom_move(swap_jokers(swap_joker2(swap_joker1(afile)))))
def cipher_函数(afile):
def swap_joker1(文件):
idx=文件索引(27)
如果idx==len(afile)-1:
afile[idx],afile[0]=afile[0],afile[idx]
其他:
afile[idx],afile[idx+1]=afile[idx+1],afile[idx]
返回文件
def swap_joker2(文件):
idx=文件索引(28)
如果idx=len(afile)-2和idx=len(afile)-1:
afile[idx],afile[idx+1],afile[idx+2]=afile[idx+1],afile[idx+2],afile[idx]
elif idx==len(afile)-2和idx=len(afile)-1:
a=afile.pop(idx)
a文件插入(1,a)
艾利夫·艾德克斯=len(afile)-2和idx==len(afile)-1:
a=afile.pop(idx)
a文件插入(2,a)
返回文件
def swap_小丑(afile):
idx1=文件索引(27)
idx2=文件索引(28)
a=a文件[0:idx1]
b=文件[0:idx2]
如果len(a) elif len(a)如果你是为了一个特殊情况而做的,比如说swap\u joker2(swap\u joker1(afile))
那么,这可能有点困难,但我想你不是在尝试
如果您的问题是swap\u joker2(afile)
不是27或28,请记住此数字并返回到步骤1(swap\u joker1)。如果这个结果不是27或28,重复步骤1,那么你的函数定义应该是递归的
您的spaw_小丑1和swap_小丑2应该如下所示
def swap_joker1(afile):
idx = afile.index(27)
if idx == len(afile) - 1:
afile[idx],afile[0]=afile[0],afile[idx]
else:
afile[idx],afile[idx+1]=afile[idx+1],afile[idx]
# Add the recursive code
if not afile in {27, 28}: # Check the resulting afile & act accordingly
return swap_joker1(afile)
else:
return afile
def swap_joker2(afile):
idx = afile.index(28)
if idx!=len(afile)-2 and idx!=len(afile)-1:
afile[idx],afile[idx+1],afile[idx+2]=afile[idx+1],afile[idx+2],afile[idx]
elif idx==len(afile)-2 and idx!=len(afile)-1:
a=afile.pop(idx)
afile.insert(1,a)
elif idx!=len(afile)-2 and idx==len(afile)-1:
a=afile.pop(idx)
afile.insert(2,a)
# Check the resulting afile & act accordingly
if not afile in {27, 28}:
return swap_joker1(afile)
else:
return afile
您的缩进错误,swap_joker2的elif与if不在同一级别。