异常后留在 while 循环中

Stay in while loop after an exception

我希望用户通过输入要查找的 ip 地址来停留在 while 循环中,如果发生异常,如果他们想继续输入 y 或 n,他们可以在出现提示时继续查找 ip通常是因为没有找到所有的 IP 地址,我陷入了无限循环,所以我暂时让它脱离了循环。我如何在异常发生后继续询问用户是否想查找另一个 IP 而不是跳出循环?谢谢

import socket

print("\n----------Look up Domain by IP Address----------\n")

response3 = input("Enter an IP Address: ")

while True:
    try:
        domain = socket.gethostbyaddr(response3)[0]#.split(".")[1]
        print("\nDomain Name is", domain)

    except(socket.error):
        print("\nA domain name could not be found.")
        break

    response4 = input("Would you like to look up another IP adress? type y for [yes] or n for [no]: ")            

    if response4 == "y":

        response3 = input("Enter an IP Address: ")

    elif response4 == "n":
        print("\n[END]")
        break  

不要使用[break],[break]表示跳出循环。使用[继续]开始循环的下一步。

如果想在第一个[try]中继续到第二个[try],可以把第一个[try]中的[break]改成[pass],也就是什么都不做。