What I want to do is validate user inputs. The criterion is only numeric inputs are allowed, no alpha, no characters like .,/?<> etc.
Say a user inputs 1989, it will print true
But if the user inputs anything other than numeric characters, say 1989a or a1989 etc, even one character will suffice for the function to print false, then ask the user for another input.
I tried something like this:
import re
def ask_number() -> None:
# prompt positive int inputs
while True:
card = input("Number: ")
# non numeric input
if re.search("![0-9]", card):
print("false")
continue
print("true")
break
ask_number()
but it prints "true" for some reason.
Then I also tried using findall:
# non numeric input
if re.findall("![0-9]", card):
print("false")
continue
same result.
Then I tried the caret:
# non numeric input
if re.findall("^![0-9]", card):
print("false")
continue
still to no avail. It seems like the bang didn't do anything. I, myself haven't quite mastered regular expressions use in Python. Any help would be appreciated.
!is not a thing for regular expressions. You want[^0-9]instead.\Dalready means "anything that isn't a digit" so if a string matches\D, it's not purely numerical.