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")
4
  • you can use a regex for this (regular expression), see stackoverflow.com/questions/8586346/python-regex-for-integer Commented May 2, 2017 at 16:57
  • @ralfhtp: I don't see this question handling the commas. Commented May 2, 2017 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 ??? Commented May 2, 2017 at 17:00
  • 1
    stackoverflow.com/questions/5917082/… Commented May 2, 2017 at 17:02

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

5 Comments

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
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.
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") ?
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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.