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()
解决方案
我不确定你到底属于什么,但你可以先定义函数,然后调用一个 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"""
推荐阅读
- python - python同时解析环境变量和额外的插值
- javascript - 三角形的 WebGL 代码,但三角形不会显示
- javascript - JavaScript event.target 还是这个?
- python - Python3从文件txt文件目录列表创建JSON
- python - 无法连接到远程 MS SQL Server 并通过它访问它的数据库。Python
- python - 如何在 pandas 列的列表中搜索?
- php - 如何使用 PHP 代码进行更改/修改 HTML 代码
- c# - 如何在 MVVM xamarin 中的按钮命令之前显示选择器选择?
- opencv - 如何用计算机视觉检测脏透明水箱中的水位?
- python - 我如何在 spacy 中运行在 GPU 上训练的模型