import re test_string = 'go' #Initialize flag to exit loop while test_string != 'stop': # Get an input phone number: nnn-nnnn test_string = input('Enter a 10-digit phone number in the format xxx-xxx-xxxx: ') #Perform comparison if re.match(r'^\d{3}-\d{3}-\d{4}$', test_string): print (test_string, 'is a valid US phone number') else: if test_string != 'stop': print (test_string, 'Rejected. Invalid phone number.')