簡體   English   中英

如何回到第1步使用python中的while語句

[英]how to go back to step 1 function use while statement in 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+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)))))

如果swap_joker2(swap_joker1(afile))的結果不是27或28,則記住該數字並返回步驟1(swap_joker1),如果結果為27或28,則返回步驟1(swap_joker1)。 我對這部分感到困惑,我嘗試使用while語句,但是我不知道如何表達“回到步驟1”。 誰能幫助我?

如果您是在特殊情況下執行此操作,例如swap_joker2(swap_joker1(afile)) ,則可能會有些困難,但是我想您不只是嘗試這種方法。

如果您的問題是swap_joker2(afile)不是27還是28,請記住該數字並返回到步驟1(swap_joker1)。 並且如果此結果也不是27或28,請重復步驟1,則您的函數定義應該是遞歸的。

您的spaw_joker1和swap_joker2應該看起來像

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM