2011-12-02 69 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