python中的while_在Python中中断while循环

我有一个正在读取用户输入的while循环。但是我希望用户能够在任何时候打破while循环,这样他们就可以返回主菜单。在

目前,这是我唯一能做到的方法(如果用户在每一点后都输入了“--back”语句)。这个菜单代码从返回菜单.解析如果它被改变了。但在新菜单代码生效之前,while循环必须完全完成。有没有办法不用所有的if语句就可以做到这一点?在

menu是一个类,还有几个其他类。下面是主回路的一部分。在

一些菜单类:class Menu:

def __init__(self):

self.code = 'main'

self.codememory = []

def exit(self, input):

if input == '--exit':

sys.exit()

def back(self, input):

if input == '--back':

if 'main' in self.codememory:

print "should be going back"

print self.code

self.code = self.codememory.pop()

print self.code

def move(self, input):

if input == '--new':

self.codememory.append(self.code)

self.code = 'new'

if input == '--main':

self.codememory.append(self.code)

self.code = 'main'

if input == '--help':

self.codememory.append(self.code)

self.code = 'help'

def select(self, input):

if input in string.digits:

input = int(input)

if input == 1:

self.codememory.append(self.code)

self.code = 'new'

if input == 2:

self.codememory.append(self.code)

self.code = 'test'

def header(self, title):

os.system('clear')

print "-------------------"

print title.upper()

print "-------------------"

print ""

def parse(self, input):

self.exit(input)

self.back(input)

if self.code == 'new':

return input.lower().decode('utf-8')

############################

menu = Menu()

while 1:

...

while menu.code == 'new':

menu.header('save word')

key = menu.parse(raw_input("Norwegain: "))

while key in dict.keys:

print "word already Saved in Dictionary"

key = menu.parse(raw_input("Norwegain: "))

if menu.code == 'new':

val = menu.parse(raw_input("English: "))

if menu.code == 'new':

syn = menu.parse(raw_input("Synonym: "))

if menu.code == 'new':

ant = menu.parse(raw_input("Antonym: "))

while menu.code == 'main':

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值