How to determine when the input is alphabetic?

I tried to solve this problem for a while and cannot get it to work correctly .. 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

Now I did this when the user entered a number above 100 or less than 1, it prints "invalid". BUT what if I want to do this when the user enters a string that is not a number (alphabetic, punctuation, etc.), He also returns this "invalid" message?

I was thinking about using if not ... isdigit (), but that will not work, since I get the assumption as an integer so that the work on this range works. Try / except is another option that I was thinking about, but still have not figured out how to implement it correctly.

+3
2

:

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

, \ninvalid , , 100 1.

EDIT: , x < y < z. - , , not.

+6
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 wrong)"
      break  # out of while loop after processing
+5

Source: https://habr.com/ru/post/1770270/


All Articles