1

I would like to know the easiest way to check it the user typing in a letter versus a number. If the user types in a letter it would give them an error message and give them the question back. Right now I have it so when the user enters a 'q' it will exit the script.

if station == "q":
        break
else:
        #cursor.execute(u'''INSERT INTO `scan` VALUES(prefix, code_id, answer, %s, timestamp, comport)''',station)
        print 'Thank you for checking into station: ', station

I need it to loop back to the question asking for the station.

Marc Brigham
  • 2,114
  • 5
  • 22
  • 27
  • Are we talking command line here? – Bobby Jun 24 '11 at 14:20
  • 1
    possible duplicate of [How do I check if a string is a number in Python?](http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-in-python) –  Jun 24 '11 at 14:22
  • http://code.activestate.com/recipes/303495-check-that-a-string-represents-an-integer-number/ –  Jun 24 '11 at 14:23
  • @Sentinel I'm asking something different entirely. – Marc Brigham Jun 24 '11 at 14:23
  • 1
    @Marc Brigham: It helps to explain *why* this question is different entirely. You may know what's different. We don't. It helps to explain to us how this is unique, or different. – S.Lott Jun 24 '11 at 14:26
  • I'm sorry maybe I should be more clear. basically if the user types in A-Z caps or lower case I want it to say "sorry that is not a valid station number" and also I forgot to mention this, I need it to not include zero. so A-Z caps or no caps, and 0 have to be excluded. Hope this helps. – Marc Brigham Jun 24 '11 at 14:31
  • @Marc Brigham: It helps to **update** the question, rather than add comments to it. – S.Lott Jun 24 '11 at 17:36

3 Answers3

6

Just using python built-in method

str.isdigit()

SEE http://docs.python.org/library/stdtypes.html

e.g.

if station.isdigit():
   print 'Thank you for checking into station: ', station
else:
   # show your error information here
   pass
YeJiabin
  • 1,038
  • 1
  • 9
  • 17
0

Try this (using YeJiabin's answer)

def answer():
  station = raw_input("Enter station number: ")
  if not(str.isdigit(station)):
    answer()

Haven't tested that!

samb8s
  • 427
  • 6
  • 16
  • This worked for the A-Z and special characters. Now how about a zero? – Marc Brigham Jun 24 '11 at 14:37
  • This has the disadvantage that the user can crash the program with a maximum recursion depth exceeded error by entering lots of wrong input – Peter Jun 24 '11 at 14:37
  • @peter yeh that's true. It seemed a simple way to demonstrate the str.isdigit function. – samb8s Jun 24 '11 at 14:38
  • I thought zero **was** a digit last time I did math?! You could always insert a specific clause for zero, `if not(str.isdigit(station)) or station==0` – samb8s Jun 24 '11 at 14:39
  • yes it is but I commented on the problem I was having up above and forgot to mention in the post that a station can't be zero. Sorry about that. But what you gave me worked for most of my problem and appreciate it. – Marc Brigham Jun 24 '11 at 14:42
  • no problem. Welcome to python. You'd definitely benefit from trying out the python tutorial. You can find that easily with google :) – samb8s Jun 24 '11 at 14:43
  • @Marc Brigham sorry! `station=="0"` – samb8s Jun 24 '11 at 14:45
0

With the requirements that you're looking to see if the input string contains anything other than the digits 1..9:

>>> import re
>>> # create a pattern that will match any non-digit or a zero
>>> pat = re.compile(r"[\D0]")
>>> pat.search("12345")
>>> pat.search("123450")
<_sre.SRE_Match object at 0x631fa8>
>>> pat.search("12345A")
<_sre.SRE_Match object at 0x6313d8>
>>> def CheckError(s):
...    if pat.search(s):
...       print "ERROR -- contains at least one bad character."
... 
>>> CheckError("12345")
>>> CheckError("12a")
ERROR -- contains at least one bad character.
>>> CheckError("120")
ERROR -- contains at least one bad character.
>>> 
bgporter
  • 35,114
  • 8
  • 59
  • 65