[Tutor] Re: Problem with Exception Error

Jorge Godoy godoy at ieee.org
Sun Aug 22 22:54:16 CEST 2004


"Alan Gauld" <alan.gauld at blueyonder.co.uk> writes:

>> I know I am doing this wrong but I can't figure out how to 
>> get it to raise the error so that it will print out 
>> "You must type y or n!" 
>
> There is no error. At least, not so far as Python is concerned, 
> so the except never gets executed. Try a simple else statement:
>
> def YesNo():
>     while True:
>         yn = raw_input("\r\nWhould you like to play again? ")
>         if yn == 'y':  # might try:  if yn in 'yY' instead
>            game()
>         elif yn == 'n':   # and elif yn in 'nN'
>            print "Thank you for playing!"
>            break
>         else:
>             print "You must type y or n!"

You might want to use "yn.lower()", to grant that either Y or y
works, and the same for the 'N' and 'n' pair.


Be seeing you,
-- 
Godoy.     <godoy at ieee.org>



More information about the Tutor mailing list