2011-12-02 2 views
0

안녕하세요, 루프를 끊을 수있는 방법이 있는지 궁금 해서요, 나는 break 명령을 사용하려고했지만 아무 것도하지 않습니다. 나는 이것에 아주 새롭다.행맨 게임, 휴식 시간 나누기 및 sys.exit

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

내가 sys.exit을 시도하고 예외를 만드는 :

또한, 사람들이 사용을 명령을 사용하면 정상적으로 종료하기 위해 그것을 얻을 수있는 방법이있다.

Heres는 내 코드 :

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

줄 들여 쓰기를 확인해야합니다. 코드를 편집하여 수정하십시오. – joaquin

+0

에 4 개의 공백을 넣으면 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 모두이 프로젝트가 3 일 만에 끝날 것임을 멋지게 도왔습니다. 그게 내가 필요한 마지막 것입니다. – Addles