Python 如何停止这个程序

Python 如何停止这个程序,python,while-loop,exit,Python,While Loop,Exit,当我在空闲状态下运行此程序并键入0作为响应时,它会打印消息,但不会停止程序。我以为将keepGoing设置为False可以阻止它,但我不知道发生了什么。请帮忙 """ crypto.py Implements a simple substitution cypher """ alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" key = "XPMGTDHLYONZBWEARKJUFSCIQV" def main(): keepGoing = True whi

当我在空闲状态下运行此程序并键入0作为响应时,它会打印消息,但不会停止程序。我以为将keepGoing设置为False可以阻止它,但我不知道发生了什么。请帮忙

""" crypto.py
Implements a simple substitution cypher
"""

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key =   "XPMGTDHLYONZBWEARKJUFSCIQV"

def main():
  keepGoing = True
  while keepGoing:
    response = menu()
    if response == "1":
      plain = input("text to be encoded: ")
      print(encode(plain))
    elif response == "2":
      coded = input("code to be decyphered: ")
      print (decode(coded))
    elif response == "0":
      print ("Thanks for doing secret spy stuff with me.")
      keepGoing = False
    else:
      print ("I don't know what you want to do...")
    return main()

def menu():
    print("Secret decoder menu")
    print("0) Quit")
    print("1) Encode")
    print("2) Decode")
    print("What do you want to do?")
    response = input()
    return response

def encode(plain):
    plain = plain.upper()
    new = ""
    for i in range(len(plain)):
        y = alpha.index(plain[i])
        new += key[y]
    return new

def decode(coded):
    coded = coded.upper()
    x = ""
    for i in range(len(coded)):
        z = key.index(coded[i])
        x += alpha[z]
    return x

main()
在退出while循环并重新启动程序之前,再次调用main():

def main():
    keepGoing = True
    while keepGoing:
        response = menu()
        if response == "1":
            plain = input("text to be encoded: ")
            print(encode(plain))
        elif response == "2":
            coded = input("code to be decyphered: ")
            print (decode(coded))
        elif response == "0":
            print ("Thanks for doing secret spy stuff with me.")
            keepGoing = False
        else:
            print ("I don't know what you want to do...")
#      return main()  # <-- delete this line

实际上,他在重复
while
循环之前调用它。是的,我刚刚注意到,我被2个空格的缩进愚弄了。谢谢。当递归被删除时,一个简单的
返回将结束程序。不需要
keepGoing
。非常感谢您,如果您想在退出之前做些什么,那么
break
语句将结束循环。我希望我知道是谁在教人们使用
while:
而不是
while:
。尝试提出一个值错误或使用break语句。
def main():
    while True:
        response = menu()
        if response == "1":
            plain = input("text to be encoded: ")
            print(encode(plain))
        elif response == "2":
            coded = input("code to be decyphered: ")
            print (decode(coded))
        elif response == "0":
            print ("Thanks for doing secret spy stuff with me.")
            break
        else:
            print ("I don't know what you want to do...")