簡體   English   中英

我有一個運行函數的while循環,如何在if之后跳出該函數並返回while循環?

[英]I have a while loop that runs a function, how do I break out of the function after an if and return to the while loop?

我正在嘗試在無限循環內調用函數。 一旦x在函數中發生,我希望它返回while循環的開始。 您如何突破功能?

and but, no luck. 我嘗試使用但是沒有運氣。

def cmndln():
    while True:
        command = input("Input:> ")
        if command == 'exit':
            clientsocket.close()
            break

        elif command == 'decrypt':
            clientsocket.send("decrypt".encode())
            msg = clientsocket.recv(1024)
            print(msg.decode())
            key = input("Key: ")
            clientsocket.send(key.encode())
            msg = clientsocket.recv(1024)
            print(msg.decode())
            command = None
            break

        elif command == 'encrypt':
            clientsocket.send("encrypt".encode())
            msg = clientsocket.recv(1024)
            print(msg.decode())
            command = None
            break
    return

while True:
    cmndln()

我想再次啟動該功能

def cmndln():
    command = input("Input:> ")
    if command == 'exit':
        clientsocket.close()

    elif command == 'decrypt':
        clientsocket.send("decrypt".encode())
        msg = clientsocket.recv(1024)
        print(msg.decode())
        key = input("Key: ")
        clientsocket.send(key.encode())
        msg = clientsocket.recv(1024)
        print(msg.decode())
        command = None

    elif command == 'encrypt':
        clientsocket.send("encrypt".encode())
        msg = clientsocket.recv(1024)
        print(msg.decode())
        command = None

def main():
    while True:
    #condition here
         cmndln()

if __name__ = '__main__':
     main()

您可以在主函數中調用一次,而不是無限循環兩次。

這樣,命令仍像以前一樣被調用。 現在,您可以在一個條件上中斷存在該功能。

暫無
暫無

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

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