Check for invalid input

I am writing a program that will take a string as input and check if the elements in it are valid or not. I want my input to contain only lower characters and periods, exclamation points and space, and not an empty string. If the user enters an empty string or an invalid character, they will be asked to enter the string again:

I know how to check a character in a string. I use this method

alpha ="abcdefghijklmnopqrstuvwxyz" message= input("Enter message: ") for i in message: if i in alpha: print i 

I usually use the method below to check for invalid input, but it will not work for this case if I want to check a character in a string. I can only use this to check if the message is empty.

 textOK = False while not textOK: message= input(prompt) if len(message) == 0: print("Message is empty) else: textOK= True 

This will prompt the user when they enter an empty string. I do not know how to combine the two methods. In short, I want to check if my input contains only lowercase letters, periods, exclamation points, and a space. If it contains other special characters or numbers or is an empty string, the user will be prompted to enter the message again. Please, help!

+6
source share
4 answers

You can use the allowed character set to check if the set is an add-on for the entered string:

 allowed = set("abcdefghijklmnopqrstuvwxyz! .") while True: message = input("Enter message: ") if message and allowed.issuperset(message): # do whatever break print("Invalid characters entered!") 

This will only allow what is allowed:

 In [19]: message = "foobar!!$ " In [20]: message and allowed.issuperset(message) Out[20]: False In [21]: message = "foobar!! " In [22]: message and allowed.issuperset(message) Out[22]: True In [23]: message = "" In [24]: bool( message and allowed.issuperset(message)) Out[24]: False 

You can also use all()... :

 while True: message = input("Enter message: ") if message and all(ch in allowed for ch in message): print("ok") break print("Invalid characters entered!) 

If you want to output bad characters:

 while True: message = input("Enter message: ") if message and all(ch in allowed for ch in message): print("ok") break print("The following invalid character(s) were entered! {}" .format(" ".join(set(message)- allowed))) 
+4
source
 import re while True: txt = input('Enter message: ').strip() if re.match(r'^[az\. !]+$', txt): break print('Mistakes:', re.findall(r'[^az\. !]', txt)) print('correct!') 
+2
source

Take a look at the string.punctuation python function. You can also set which inputs are allowed / not allowed.

+1
source

A more advanced regular expression that allows only valid sentences:

 import re while True: txt = input('Enter message: ').strip() if re.match(r'^[az]+[\.\!]?([ ][az]+[\.\!]?)*$', txt): break 

will allow:

 ipsum. asd. dolor sit amet consectetur adipiscing elit! aenean libero risus consequat ac felis eget aenean facilisis tortor nunc et sollicitudin ut augue mauris tincidunt ut felis id mattis fusce vel diam nec tellus consequat lacinia 

will not allow:

 two spaces hey!! no!. !me ! ! ! 
+1
source

All Articles