2011-12-02 59 views
0

你好,我想知道是否有什麼辦法可以有一個循環被打破,我試圖用break命令,但並不做任何事情。我對此很新。劊子手遊戲,環斷裂和sys.exit

此外,有沒有什麼辦法讓它正常關閉,當人們使用使用命令:

wannaplay = raw_input('Wanna play hangman?(yes or no): ')" 

我試過sys.exit這使得例外。

繼承人我的代碼:

import random 
import sys 

play = 'yes' 
while play is 'yes': 
word = ['adult','pen','apple'] 
secret = random.choice(word) 
guesses = '' 
turns = 5 
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", 
     'adult','pen','apple'] 
wannaplay = '' 
done = False 

while(done is False): 
    wannaplay = raw_input('Wanna play hangman?(yes or no): ') 
    while(done is not True): 
     if (wannaplay == 'yes'): 

       while turns > 0: 
        missed = 0 
        for letter in secret: 
         if letter in guesses: 
          print letter, 
         else: 
          print '_', 
          missed += 1 

        print 

        if missed == 0: 
         print 'You win!' 
         done = True 
         break 

         break 

        guess = raw_input('guess a letter: ') 
        guesses += guess 
        if guess not in alphabet: 
         print 'error: Not a letter' 
        else: 
         break 

         if guess not in secret: 
          turns -= 1 
          print 'Nope.' 
          print turns, 'more turns' 
          if turns == 0: 
           print 'The answer is', secret 
          else: 
           done = True 
           break 

     else: 
      done = True 
      break 
+0

你應該檢查你的行縮進。請嘗試編輯您的代碼並更正它。使用4個空格縮進 – joaquin

+0

sys.exit()給你什麼異常?在我修改你的間距後,我沒有任何問題。 http://pastebin.com/VMsEm0cK – cdated

回答

1

使用異常打破循環。考慮以下示例

try: 
    while True: 
     answer = raw_input("Type [Y]es to Exit :") 
     if answer.lower() in ["yes","y"]: raise StopIteration 
     print "Your answer is ", answer 
except StopIteration: 
    print "Good Bye" 


Type [Y]es to Exit :No 
Your answer is No 
Type [Y]es to Exit :Why 
Your answer is Why 
Type [Y]es to Exit :I Won't 
Your answer is I Won't 
Type [Y]es to Exit :Ok 
Your answer is Ok 
Type [Y]es to Exit :Yes 
Good Bye 

實際上,您可以在多個級別組合多個Exit。考慮下一個例子

try: 
    while True: 
     answer = raw_input("Type [Y]es to Exit :") 
     if answer.lower() in ["yes","y"]: raise StopIteration 
     print "Your answer is ", answer 
     try: 
      n=0 
      while True: 
       n+=1 
       answer = raw_input("Your Name? (Type [E]xit to Quit) :") 
       if answer.lower() in ["exit","e"]: raise StopIteration 
       print "Nice To Meet you", answer 
       if n>=5: StopIteration 
     except StopIteration: 
      None 

except StopIteration: 
    print "Good Bye" 


Type [Y]es to Exit :No 
Your answer is No 
Your Name? (Type [E]xit to Quit) :Jon 
Nice To Meet you Jon 
Your Name? (Type [E]xit to Quit) :Janny 
Nice To Meet you Janny 
Your Name? (Type [E]xit to Quit) :George 
Nice To Meet you George 
Your Name? (Type [E]xit to Quit) :E 
Type [Y]es to Exit :I Won't 
Your answer is I Won't 
Your Name? (Type [E]xit to Quit) :A 
Nice To Meet you A 
Your Name? (Type [E]xit to Quit) :B 
Nice To Meet you B 
Your Name? (Type [E]xit to Quit) :C 
Nice To Meet you C 
Your Name? (Type [E]xit to Quit) :D 
Nice To Meet you D 
Your Name? (Type [E]xit to Quit) :E 
Type [Y]es to Exit :YES 
Good Bye 
+0

噢,感謝joaquin和Abhijit,他們都在三天內幫助我完成了這個項目,這是我需要的最後一件事。 – Addles