[Tutor] Problem with Exception Error with rest of code

Isr Gish isrgish at fastem.com
Mon Aug 23 19:57:44 CEST 2004


Hi Kevin,

-----Original Message-----
   >From: "Kevin"<python_newbie at vedorian.com>
   >Sent: 8/22/04 3:52:50 PM
   >To: "tutor at python.org"<tutor at python.org>
   >Subject: Re: [Tutor] Problem with Exception Error with rest of code
     >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()

The line above is calling a 2nd time the YesNo function which onlterMenates wHen you press 'n'. Therfore I think you should just take it out from here and it should work fine.

All the best
Isr

   >                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
   >>
   >>
   >>
   >>  
   >>
   >
   >_______________________________________________
   >Tutor maillist  -  Tutor at python.org
   >http://mail.python.org/mailman/listinfo/tutor
   >



More information about the Tutor mailing list