[Tutor] Problem with Exception Error with rest of code

Kevin python_newbie at vedorian.com
Sun Aug 22 21:52:50 CEST 2004


Here is the entire code if that will help:
import random

def game():
    rand = random.randrange(1, 10)
    while True:
        x=raw_input("Enter a number: ")
        try:
             if int(x) != rand:
                print "\r\nThat's not it!\r\n"
             if int(x) == rand:
                print "\r\nYAY! you guessed it right!"
                YesNo()
                break
        except ValueError:
             print "\r\nYour guess must be a number!\r\n"
       
def YesNo():
     while True:
         yn = raw_input("\r\nWhould you like to play again? ")
         if yn == 'y':
             game()
         elif yn == 'n':
             print "Thank you for playing!"
             break
         else:
             print "You must type y or n!"
YesNo()


Max wrote:

>On Sunday 22 August 2004 19:04, Kevin wrote:
>  
>
>>How would I fix this to get it to work
>>the way I want it to?
>>    
>>
>
>Instead of the try/except, just use an else:
>
>  
>
>>def YesNo():
>>    while True:
>>        yn = raw_input("\r\nWhould you like to play again? ")
>>        if yn == 'y':
>>            game()
>>        elif yn == 'n':
>>            print "Thank you for playing!"
>>            break
>>        else:
>>            print "You must type y or n!"
>>    
>>
>
>This way, if yn is neither 'y' nor 'n' it will print "You must type y or n!".
>
>hth, Max
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>  
>



More information about the Tutor mailing list