python newbie

djw donald.welch.nospam at hp.com
Fri Jan 9 15:42:14 EST 2004


Joe Francia wrote:

> d wrote:
>> Worked fine for me on Linux... made two suggested changes: 1) use
>> raw_input(), not input(), 2) check user input for errors. Sorry, I don't
>> know how to say "Enter a number between 0 and 500" in whatever language
>> this is in (German?)
>> 
> 
> You're a Python user and you don't recognize Dutch?!?!  For shame... ;>)

Well, I was only off by one country - not too bad for someone who took 4
years of high school Spanish and can hardly speak a word of it. 

I was looking at the code that I proposed to the OP, and wondered if this
wasn't better:

#!/usr/bin/env python
while 1:
    bedrag = raw_input ( 'Geef bedrag tussen 0 en 500 eurocent: ' )
    try:
        bedrag = int( bedrag )
        if bedrag < 0 or bedrag > 500:
            raise ValueError
    except ValueError:
        print "Enter a number between 0 and 500" # In DUTCH, of course!
    else:
        break
    
# ... etc.


Is this good style, that is, to put multiple items inside a single try:
except: else: block? Seems more compact and tidy than my first try.

-d






More information about the Python-list mailing list