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?
2 Answers
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 :))
1 Comment
Jai Sloper
ok let me know what happens
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
.isnumeric(),.isdigit().