[Tutor] Re: Eceptions

Diana Hawksworth dianahawks at optusnet.com.au
Sat Apr 16 23:39:22 CEST 2005



>     From: "Brian van den Broek" <bvande at po-box.mcgill.ca>
>     To: "Diana Hawksworth" <dianahawks at optusnet.com.au>
>     Cc: <tutor at python.org>
>     Sent: Saturday, April 16, 2005 3:14 PM
>     Subject: Re: [Tutor] exceptions
>     > Diana Hawksworth said unto the world upon 2005-04-15 22:25:
>     > > Hello list,
>     > >
>     > > I have been trying to trap a string entry by raising an exception.
The
>     code follows - but the exception is never raised.  What am I doing
wrong?
>     > >
>     > > TIA Diana
>     > >
>     > >  try:
>     > >
>     > >             self.guess = int(self.num_ent.get())
>     > >
>     > >             self.num_ent.delete(0,END)
>     > >             self.num_ent.focus_set()
>     > >
>     > >
>
> - Ignored:
>     > >             if self.guess < self.number:
>     > >                 message = str(self.guess) + " is too low. You need
to
>     guess higher"
>     > >
>     > >             if self.guess > self.number:
>     > >                 message = str(self.guess) + " is too high. You
need to
>     guess lower"
>     > >
>     > >             if self.guess == self.number:
>     > >                 message = str(self.guess) + " is the correct
number!"
>     > >                 self.message_txt.config(state = NORMAL)
>     > >                 self.message_txt.config(state = DISABLED)
>     > >
>     > >             self.message_txt.config(state = NORMAL)
>     > >             self.message_txt.delete(0.0, END)
>     > >             self.message_txt.insert(0.0, message)
>     > >             self.message_txt.config(state = DISABLED)
>     > >
>     > >
>     > >  except(ValueError):
>     > >          message = str(self.guess) + " is not a number. Please try
>     again!"
>     > >
>     >
>     > Hi Dianna,
>     >
>     > What are you expecting to raise the ValueError? It looks to me like
it
>     > must be the line:
>     >
>     > self.guess = int(self.num_ent.get())
>     >
>     > But, if that is so, then the assignment to message in your except
>     > clause won't work, as self.guess won't have been assigned.
>     >
>     > I'm no expert, so I may be missing something, but I wouldn't be
>     > surprised if your num_ent.get() has details that are pertinent. What
>     > does that method return?
>     >
>     > And, while I'm writing, I'd suggest rewriting your lines like:
>     >
>     > message = str(self.guess) + " is the correct number!"
>     >
>     > in this form instead:
>     >
>     > message = "%s is the correct number!" %self.guess
>     >
>     > The almost irrelevant benefit is that it is faster. The big plus is
>     > string formatting is so handy, that I think it helps a lot to get
used
>     > to using it in most cases, even those that are easy to handle with
>     > string concatenation as you have done.
>     >
>     > Best,
>     >
>     > Brian vdB
>     >
>     >
>     Thanks for your reply Brian.  The num_ent.get() method returns a
number.
>     Actually, it is an Entry box that returns a string, that I then
convert to
>     an integer.  What I would like to do, is, if a user enters a string,
to have
>     the program return an exception. At the moment it just blithely
carries on
>     as if the string that was entered was in fact an integer, and compares
that
>     entered string to a random number!
>
>     Thanks for the hint on string formatting!  I had forgotten about that!
Been doing
>     this for a month only!!!
>
>     Thanks again. Diana
>
>
>
> - Done.
>
>




More information about the Tutor mailing list