views:

100

answers:

2

I been trying to solve this one for a while and can't seem to make it work right.. here is my current work

while True:

    guess = int(raw_input('What is your number?'))

    if 100 < guess or guess < 1:
        print '\ninvalid'

    else:
        .....continue on

Right now I have made it so when a user input a number higher than 100 or lower than 1, it prints out "invalid". BUT what if i want to make it so when a user input a string that is not a number(alphabetic, punctuation, etc.) it also returns this "invalid" message?

I have thought about using if not ...isdigit(), but it won't work since I get the guess as an interger in order for the above range to work. Try/except is another option I thought about, but still havent figured out how to implement it in correctly.

+5  A: 

You can use exception handling:

try:
    guess = int(raw_input('What is your number?'))
    if not (1 <= guess <= 100):
        raise ValueError
    # .....continue on
except ValueError:
    print '\ninvalid'

That way, \ninvalid will be printed if the user either inputs a non-numeric string or inputs a numeric string greater than 100 or smaller than 1.

EDIT: Okay, I submit to the x < y < z syntax. Still think it loses some of its charm when it's used with not, though.

Frédéric Hamidi
wow thank you Frederic your code seems much more simpler. Though i have never learn what "raise" is in python, under what circumstances can you use it? and how does it benefit? thank you! and thank you to all who contributed
neogeo
@neogeo, `raise` allows you to throw exceptions yourself (see [http://python.about.com/od/gettingstarted/ss/begpyexceptions_7.htm](http://python.about.com/od/gettingstarted/ss/begpyexceptions_7.htm)). In your case, `int()` raises `ValueError` if the value passed is not numeric. If we extend that behavior by also raising `ValueError` if the value is numeric but outside our domain, we can handle the two error cases in the same place at the same time.
Frédéric Hamidi
+5  A: 
while True:
  try:
    guess = int(raw_input("..."))
  except EOFError:
    print "whoa nelly! EOF? we should probably exit"
    break  # or sys.exit, or raise a different exception,
    # or don't catch this at all, and let it percolate up,
    # depending on what you want
  except ValueError:
    print "illegal input: expected an integer"
  else:
    if not (1 <= guess <= 100):
      print "out of range"
    else:
      print "processing guess... (but if it wasn't 42, then it's wrong)"
      break  # out of while loop after processing
Roger Pate
+1 for putting a minimal number of lines between `try` and `except`, and also for using the `1 <= guess <= 100` syntax.
EOL
I like Fred's lack of duplication, but there are more Pythonic bits in this. (`x < y < z`, and bonus `else:` clauses.)
Nick T
@Nick: Often you'll handle *illegal* input (i.e. not a number) differently from *invalid* input (i.e. out of range), even if only to tell the user more information. Once that happens, there's no duplication. (Caveat emptor: That distinction in "illegal"/"invalid" as terms isn't important, I didn't even follow it originally above, and I don't know anyone that assumes the distinction unless specified. However, you can find the same idea in technical/standardese specs.)
Roger Pate