mercredi 28 mars 2018

How to take Postal Code Checker using Regular expression and Input in Python 3

My code should allow user's input to match the Regular expression of my "postal code" and print out Match found - Valid postal code. I need help to get my result, but i keep getting TypeError: expected string or bytes-like object.

import re

print("--- POSTAL CODE CHECKER PROGRAM --- ")
# user_input_1 = input('Please enter the city/province: ')
user_input_2 = input('Please enter the postal code: ')

postalCode = ["T, V, R, E, A, X, B, K, L, M, N ,P, C , G, H, J, S, Y, 0-9"]

pattern = re.compile(r'[TVREAXBKLMNPCGHJSY]\d[TVREAXBKLMNPCGHJSY] \d[TVREAXBKLMNPCGHJSY]\d')

if user_input_2 == pattern.match(postalCode):
    print('Match found - Valid postal code:{}'.format(user_input_2))
else:
    print("Error - No match found")

Aucun commentaire:

Enregistrer un commentaire