Diana Hawksworth said unto the world upon 2005-04-16 17:39:

<SNIP>

   > 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?
   >

<SNIP my suggestion to use string formatting>

   >
   > 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!


Hi Diana,

hm, our mail clients between them appear to have made a mess of the quoting :-( This makes it a bit harder to parse the code, but let's try.

I'm still not 100% clear on the role of

self.guess = int(self.num_ent.get())

in your code. Does self.num_ent.get() return a string or an integer? Perhaps I'm misreading, but you seem to say both. Your second comment is that "it is an Entry box that returns a string" -- I assume you mean that at some point it calls the raw_input function, right?

What happens if you enter something like "I am not a number!" at that point in the run of self.num_ent.get? If self.num_ent.get() really just returns a string, then I'd expect roughly the same thing as what happens below:

>>> my_input = raw_input("Well?\n")
Well?
I am not a number!
>>> int(my_input)

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in -toplevel-
    int(my_input)
ValueError: invalid literal for int(): I am not a number!
>>>

I think it would help if you posted the code for the num_ent.get method. Otherwise, I at least, am out of stuff to say :-)
(If you post again, please delete the goofy-formatted code quote above, and replace with a new copy-paste from your source.)



Thanks for the hint on string formatting! I had forgotten about that!

Been doing

   this for a month only!!!

Thanks again. Diana


Your welcome. There is a lot to keep in one's head at the beginning, isn't there? My money is on the claim that string formatting is a good habit to get into early.

Best,

Brian vdB

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to