Oct-05-2019, 02:38 PM
(This post was last modified: Oct-05-2019, 02:40 PM by Gribouillis.)
adt Wrote:If experts in python community were to keep striving in their zeal (though well intended) to justify all that existsYou're strongly mistaking, it is my deep conviction that int('4.5') must raise an exception. It gives the program a chance to do take proper action if the string happens to contain a floating value, for example
while True: s = input('Please enter the page number')) try: pageno = int(s) except ValueError: print('Error: an integer is required, not', repr(s))But if int() accepts floating value, how can I recognize a floating value to reject it? I need to test it before the conversion to int and create another way to ensure it's an integer, something like
import re while True: s = input('Please enter the page number')) if re.match('^\s*\d+\s*$', s): pageno = int(s) else: print('Error: an integer is required, not', repr(s))
adt Wrote:It would seem that statements (A) & (B) are consistent with each other, while the same could not be said for statements © & (D).Again you're mistaking about the intent of the
isnumeric()
method. It doesn't mean that the string represents a numeric value, it means that all the characters in the string are numeric characters. It is a different purpose.