On 11/09/2010 20.43, bob gailer wrote:
On 9/11/2010 12:12 PM, Roelof Wobben wrote:
...
You can't.
I made that comment in the context of the OPs function:

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

The OP thought (incorrectly) that, given for example:
x = '3.1'
float(x) - int(x) would evaluate to 0.1

In reality int(x) in this case raises an exception.
ValueError: invalid literal for int() with base 10: '3.1'

Since the expression was in a try he could not tell exactly what was
happening.

I also don't quite understand the use of raise in the try.
It took me some time to understand that OP was... me! What do you mean by OP? If it stands (as usual) for Original Poster, he (Roelof Wobben) did not write this piece of cleverly crafted code. It was me, and I'm glad to show you its internals. My aim was to exclude everything but positive integers, and so I did by EAFP programming. You pointed out that int('3.1') throws an exception, as I did not know.
                    *So what?*
My little readposint() function only needed to let POSITIVE INTEGERS go, and so it does. Everything else generates an exception, even positive floats (thanks to the raise inserted in a try), and I handle it informing the user and returning a well-defined and repeatable wrong value, that can be easily handled. Sure, I could make the funcion more efficient, and nearly all of you can make it more Pythonic, whatever that means. But I thought it was quite sufficient, for a school exercise. Note that int('7') returns, correctly, 7, while float('5.2') returns 5.2 as expected. I don't like this rough behaviour of int(), spitting out an exception if given a legitimate string representation of a float. Can some of you Tutors explain me why it must be so?

I wish and hope that Roelof will learn how to do program walkthroughs
and use the interactive prompt to solve things himself. I applaud the
patience some of you have ih hand-holding him. I don't have that
patience. I wish him to learn to fish.
I don't like fishing, but many fishermen friends of mine tell me it's something that greatly improve patience. If you lack this quality, as you said, then you're the one who should go fishing...

Hello Bob,

Oke, I try to fish.

Thank you.
I hope you will keep trying to learn programming and Python too, Roelof. You show tenacity and determination, and those (with patience, I would add) are very useful in serious programming. Anyway, remember the Python shell is your friend, make good use of it!


Are these the right conclusions ??

Roelof
You're very close, Roelof. I just wanted to produce exactly the same error message and result for any wrong value inserted, and return the right number if the right number was input by the user. So I started with (int(x) < 0) to get rid of all that int() cannot handle (I never knew it couldn't handle "3.2", but for now it's OK) and also negative integers (and negative floats, I thought); then I found a way to exclude positive floats. I think this is what(float(x) - int(x) > 0) can do. I pseudo code, my function is:

input value
if (value is not a positive integer): throw exception
else: return value.
exception: inform user
           return -1.

Francesco
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3130 -  Data di rilascio: 
09/12/10 08:34:00
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to