如何在python中打破循环内部的循环?

时间:2014-06-21 16:22:53

标签: python

此代码是一个回顾词汇单词的循环。出于某种原因,我的休息时间没有了。

以下是本程序使用的词语:

Vocab ={'Adherent' : "a person who follows or upholds a leader, cause, etc.; supporter; follower.",
'Incoherent' : "without logical or meaningful connection; disjointed; rambling",
'Inherent' : "existing in someone or something as a permanent and inseparable element, quality, or attribute" ,
'DiffuseADJ' : "characterized by great length or discursiveness in speech or writing; wordy"}

这是我的循环:

while 1:
    ques1= raw_input("Would you like a list of the vocabulary words, or would you like to play a game? Type 'words' or 'game': ")
    if ques1 == 'words':  
        ques11= raw_input("Type 'w' for words only, type 'wd' for words and definitions, type 'd' for definitions only: ")
        if ques11 == "w":
            for key,value in Vocab.iteritems():
                print key
        elif ques11 == "wd":
            for key,value in Vocab.iteritems():
                print key,"-", value
        elif ques11 == "d":
            for key,value in Vocab.iteritems():
                print value
    elif ques1 == 'game':
        game=raw_input("Type 'rw' for random words: ")
        if game == 'rw':
            while 1:
                y = random.choice(Vocab.keys())
                print y
                t2=raw_input("What is the definition?: ")
                if t2 in Vocab[y]:
                    print 'All those words were in the definition!'
                    print Vocab[y]
                elif t2 not in Vocab[y]:
                    print Vocab[y]
                elif t2 == 'menu': break
                raw_input("Hit 'enter': ") 








    else:
        raw_input("Hit 'enter': ")

由于某种原因,游戏循环的中断不会返回到问题'。为什么不能解决这个问题呢?

1 个答案:

答案 0 :(得分:1)

您的代码存在的问题是,如果break不在t2中,则永远不会达到Vocab语句。你可以改变

elif t2 == 'menu': break

if t2 == 'menu': break