[Tutor] Please critique my Fraq.py (original) (raw)

Dick Moores rdm at rcblue.com
Sun Jul 25 11:29:33 CEST 2004


I've had various responses to my request for a critique of my Frac.py (I mispelled it as Fraq.py in the subject header--must have had Iraq on my mind.)

Before I begin to try to implement your valuable suggestions for Frac.py, I'd like to try again for a response to a specific question I asked in that post:

In Frac.py I wanted to give the user a chance to quit at any prompt (and have the program close smoothly), by entering a "q" or an "x". I did this by the statement,

if answer in ["q", "x"]: break

The problem with this is that it only breaks out of the inner loop. I have to repeat this statement in the outer loop.

So I'm asking if there's a better way. Raising an exception doesn't do it. Is there a way (other than mine) to enable the user to quit smoothly when he's inside a loop which is inside a loop?

Here's a silly game/test script I wrote to try to illustrate my question: """ print "This is an appropriate response test." print "Enter q or x to give up." while True: while True: answer = raw_input("How are you today: ") if answer == "x": print "Give up? O.K., you can try again tomorrow." break elif answer != "Fine, thank you. And you?": print "C'mon. That's dumb. Try again." continue else: print "Congratulations! You passed the test!" break break "" At two points in the inner loop I've got a break. But I need another at the end of the outer loop to get out smoothly.

Thanks,

Dick Moores



More information about the Tutor mailing list