在python中,如何在循环内部中断循环?

2024-09-30 00:23:12 发布

您现在位置: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': ")

由于某些原因,游戏循环的中断不会返回到“ques1”。为什么这个休息时间不起作用?你知道吗


Tags: orkeyingameforinputrawif
1条回答
网友
1楼 · 发布于 2024-09-30 00:23:12

代码的问题是,如果t2不在Vocab中,那么break语句将永远无法到达。你可以改变

elif t2 == 'menu': break

if t2 == 'menu': break 

相关问题 更多 >

    热门问题