On Thu, Jun 19, 2014 at 1:23 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Thu, Jun 19, 2014 at 12:48 AM, Nicholas Cannon > <nicholascann...@gmail.com> wrote: >> On Thursday, June 19, 2014 1:53:31 PM UTC+8, Nicholas Cannon wrote: >>> I am making a calculator and i need it to support floating point values but >>> i am using the function isnumeric to check if the user has entered an int >>> value. I need the same for floating point types so i could implement an or >>> in the if statement that checks the values the user has entered and allow >>> it to check and use floating points. If you need the source code i am happy >>> to give it to you. Thank you for your help >> >> I am using python 2.7.7 and i have come up with away but there is still >> possible errors for this. What i did was i this >> >> #checks if the user input is an integer value >> def checkint(a): >> if a.isnumeric(): >> return True >> else: >> if a.isalpha(): >> return False >> else: >> return True >> >> The parameter a is the users input by the raw_input function. I first test >> if it is normal int with the isnumeric function. Unfortunately this function >> picks up the decimal as false. This means if the user inputs a float it has >> to be false. I then test if this input has any alphabetical characters if it >> does not the user could have only entered something like 12.5 oppose to >> abc.d. > > unicode.isalpha does not test if the input has *any* alphabetic > characters. It tests if the input is *only* alphabetic characters. > u'12.5'.isalpha() does return False. u'abc.d'.isalpha() *also* > returns False, because the decimal point is not alphabetic.
Incidentally, unicode.isnumeric is probably not what you want either. According to the docs, it returns "True if there are only numeric characters in S, False otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH." So that includes strings like u'123⅕⅓Ⅷ٤', which is clearly not an integer. You'd likely do better with unicode.isdigit, and even then you'd be allowing for mixed scripts. -- https://mail.python.org/mailman/listinfo/python-list