I typed is digit function for user input, but when I enter an alphabet, it accepts the value

Advertisements

            IDcheck = input("Enter The Customer ID ")
            if IDcheck.isdigit :
                print("Your ID is Being Searched")

            elif IDcheck.isalpha :
                print("IDs Only Contains Numbers")

I made a code to search customers names by ID . typed isdigit function to make sure the user enters a number
but when I type a letter, it does accept it. why

>Solution :

String.isdigit is a function.

When the interpreter executes the line if IDcheck.isdigit: it checks if a function or variable called IDcheck.isdigit exists. IDcheck.isdigit exist and it returns True. The same applies to IDcheck.isalpha.

To fix this: change IDcheck.isdigit and IDcheck.isalpha to IDcheck.isdigit() and IDcheck.isalpha() by adding parentheses, as in:

IDcheck = input("Enter The Customer ID ")
if IDcheck.isdigit():
    print("Your ID is Being Searched")

elif IDcheck.isalpha():
    print("IDs Only Contains Numbers")

Leave a ReplyCancel reply