0

I've followed the directions indicated on this link using the method .isdigit() to check if a string is a number, and it fails for me.

I am simply trying:

print("final weight:", weight)
if weight.isdigit() == True:
    print("yes")
if weight.isdigit() == False:
    print("weight is not a digit")

It prints:

final weight:  1,873
weight is not a digit

I am very confused why this fails because 1,873 is in fact a number.

UPDATE:

Using RegEx worked. I simply do:

regnumber = re.compile(r"(\d),(\d) | (\d)")    #looks for numbers with commas and numbers without commas
if regnumber.search(weight):
    print("weight = an int")
Community
  • 1
  • 1
theprowler
  • 3,138
  • 11
  • 28
  • 39
  • you can use a regex for this (regular expression), see http://www.stackoverflow.com/questions/8586346/python-regex-for-integer – ralf htp May 02 '17 at 16:57
  • @ralfhtp: I don't see this question handling the commas. – Prune May 02 '17 at 16:59
  • I took a couple of minutes to look through possible duplicates. I can't find an existing question that deals with parsing a comma-enhanced input integer. Is this *really* the first time someone has asked and let the question stand ??? – Prune May 02 '17 at 17:00
  • 1
    http://www.stackoverflow.com/questions/5917082/regular-expression-to-match-number-with-or-without-commas-and-decimals-in-text – ralf htp May 02 '17 at 17:02

1 Answers1

3

That's not a legal number in Python: it has a comma in the middle. You and I recognize it, of course, but most computer languages don't allow commas in numbers like this.

If you need to recognize such a number, try searching on regular expressions for number recognition, such as here and here.

Second reference is from ralf htp's comment on the main question.


STYLE UPDATE:

Checking a Boolean expression against a Boolean constant is redundant. Just use the value in place:

if weight.isdigit():
    print("yes")
else:
    print("weight is not a digit")
Community
  • 1
  • 1
Prune
  • 76,765
  • 14
  • 60
  • 81
  • Wow ok I feel dumb now. Sorry for muddying StackOverflow with such a novice question. Thanks a ton for responding though and directing me to a solution – theprowler May 02 '17 at 16:53
  • 2
    Congratulations: you're human. I expect that other people will have this problem ... and that I've probably missed this being a duplicate of an existing question. – Prune May 02 '17 at 16:55
  • So following that link you provided, is it as simple as `regnumber= re.compile(r"(\d),(\d)", r"\1.\2") if regnumber.search(weight): print("yes")` ? – theprowler May 02 '17 at 17:46
  • I *think* so. I haven't used regular expressions in Python; I have local packages that do the conversions I need. Try it and see? – Prune May 02 '17 at 17:47
  • Ok yeah so I just needed to read that link a closer. The guy who asked the question wanted to replace #'s with commas with #'s with periods; so I just took the regex that recognized #'s with commas and added a regex that recognizes plain numbers as well and it worked.. I'll update my question to provide an answer – theprowler May 02 '17 at 18:02