[Tutor] exceptions problem
Luke Paireepinart
rabidpoobear at gmail.com
Sat Sep 11 05:06:35 CEST 2010
Roleof, do you think you could stop sending HTML messages and use
plaintext instead? I usually skip over your posts because your font
is so small and hard to read that I don't even bother.
If you send plain-text messages (which is the norm on programming
mailing lists) then you leave it up to the e-mail client to render the
text.
Yes, I could turn off HTML on my end, but that is a pain and breaks
messages that I legitimately want to be HTML.
Thanks,
-Luke
On Fri, Sep 10, 2010 at 6:56 PM, bob gailer <bgailer at gmail.com> wrote:
> On 9/10/2010 2:48 PM, Roelof Wobben wrote:
>
>
> Date: Fri, 10 Sep 2010 20:23:09 +0200
> From: fal at libero.it
> To: tutor at python.org
> Subject: Re: [Tutor] exceptions problem
>
> On 10/09/2010 18.12, Roelof Wobben wrote:
>> ...
>> def readposint():
>> x = raw_input("Please enter a positive integer :")
>> try:
>> if not (x == int(x) and x< 0): raise(ValueError)
>> except:
>> print x , "is not a positive integer. Try again."
>> return False
>> return True
>>
>> y = readposint()
>> print y
>> while y == False:
>> readposint()
>> print "You have entered : ", y
>>
>> But -9 and 2 are both true.
> My fault, I didn't notice that after raw_input, whatever you enter is a
> STRING, not an integer! So, without any exception thrown, the comparison
> x == int(x) is always False. Let's make it better:
> if (int(x)<0 or (float(x) - int(x) > 0)): raise(ValueError)
>
>
> Then, if the input value x is indeed a positive integer, you should
> return x, not True or False. Try returning -1 if the exception is
> thrown, in line 7, and returning x in line 8. Then, you should change
> also line 12... ok, here's to you:
>
> def readposint():
> x = raw_input("Please enter a positive integer :")
> try:
> if (int(x)<0 or (float(x) - int(x) > 0)): raise(ValueError)
> except:
> print x , "is not a positive integer. Try again."
> return -1
> return x
>
> y = readposint()
> print y
> while y == -1:
> readposint()
> print "You have entered : ", y
>
>>
>> Roelof
> Francesco
>
> Thank you.
>
> I never thought that you can use a float and a integer to look if the number
> is a integer.
>
> You can't.
>
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list