0

so if you add a entry to window in Tkinter , user can write numbers , letters and symbols in it. how can i check if the input from entry is number and its not a letter or symbol?

1
  • 3
    There are functions on string to check that: .isnumeric(), .isdigit(). Commented Mar 3, 2022 at 7:08

2 Answers 2

2

you could do something like this

s = # whatever the user input
s_is_an_int = True

try: 
    int(s)
    s_is_an_int = True
except ValueError:
    s_is_an_int = False

hope this helps :))

Sign up to request clarification or add additional context in comments.

1 Comment

ok let me know what happens
0

Try this: Snippet:

def check_user_input(input):
    try:
        # Convert it into integer
        val = int(input)
        print("Input is an integer number. Number = ", val)
    except ValueError:
        try:
            # Convert it into float
            val = float(input)
            print("Input is a float  number. Number = ", val)
        except ValueError:
            print("No.. input is not a number. It's a string")


input1 = input("Enter your Age ")
check_user_input(input1)

input2 = input("Enter any number ")
check_user_input(input2)

input2 = input("Enter the last number ")
check_user_input(input2)

Result:

Enter your Age 25
Input is an integer number. Number =  25
Enter any number Parsa Ad
No.. input is not a number. It's a string
Enter the last number 

Comments

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.