Hangman游戏,循环中断和sys.exit

时间:2011-12-02 18:11:23

标签: python

您好我想知道是否有任何方法可以让循环被破坏,我试图使用break命令,但这没有做任何事情。我很陌生。

此外,当人们使用命令时,有没有办法让它优雅地关闭:

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

1 个答案:

答案 0 :(得分:1)

使用Exception打破循环。请考虑以下示例

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