如何打破这个无限循环

2024-09-29 22:32:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个菜单。这样做的目的是让脚本在选择第二个时循环回到主菜单。它循环回来,但当我选择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()

Tags: importinputrawifisdef菜单back
1条回答
网友
1楼 · 发布于 2024-09-29 22:32:24

我不确定你到底是关于什么的,但是你可以先定义函数,然后只调用一个原始输入来指示输入是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"""

相关问题 更多 >

    热门问题