繁体   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