2014-06-21 46 views
-1

此代碼是一個回顧詞彙單詞的循環。出於某種原因,我的休息不起作用。如何打破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」 。爲什麼這不能工作?

+0

怎麼把你的代碼是包裹在'而1'。即使它在內部循環中「休息」,它仍然會繼續在外部,而 –

+0

@Pilot我想它回到原來的問題,但它不會 – user2757442

回答

1

您的代碼存在的問題是,如果t2不在Vocab中,則break語句將永遠不會到達。您可以更改

elif t2 == 'menu': break 

if t2 == 'menu': break 
+0

這個作品,謝謝 – user2757442