首页 > 解决方案 > 如何打破这个无限循环

问题描述

我正在尝试创建一个菜单。这个想法是在选择第二个时让脚本循环回到主菜单。它会循环回来,但是当我选择数字 1 时,它只会一直循环。我该如何解决。

import os
import sys

def mainmenu():
    print """

this is a test menu

"""
mainmenu()

choice = raw_input("DMF>>")
if choice == '1':
   def men1():
       print """

 this is the second menu

 """

 men1()

 def back():
     men1_actions['mainmenu']()

 men1_actions = {
      'mainmenu': mainmenu,
      '2': back,
      }

 while True:
       choice2 = raw_input("DMF>>")
       if choice2 == '2':
          back()

标签: python

解决方案


我不确定你到底属于什么,但你可以先定义函数,然后调用一个 raw_input 来指示输入是 1 还是 2。

def mainmenu():
    print """this is a test menu"""
def men1():
    print """this is the second menu"""
while True:
    choice = raw_input("DMF>>")
    if choice == '1':
        mainmenu()
    elif choice == '2':
        men1()
    else:
        print """Please choose between 1 or 2"""

推荐阅读