Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Task 2
- by Mark Wu
- Format: Ull000
- Here's two different solutions to this question,
- using either Built-in functions or Regular Expression
- '''
- # --------------------Task 2.1--------------------
- '''
- UserID <-- ''
- PRINT "Please input UserID:"
- INPUT UserID
- IF LENGTH(UserID) = 6 THEN
- IF LEFT(UserID, 1) = UCASE(LEFT(UserID, 1)) AND
- MID(UserID, 2, 2) = LCASE(MID(UserID, 2, 2)) AND
- (ASC(RIGHT(UserID, 1)) >= 48 AND ASC(RIGHT(UserID, 1)) <= 57) AND
- (ASC(MID(UserID, 4, 1)) >= 48 AND ASC(MID(UserID, 4, 1)) <= 57) AND
- (ASC(MID(UserID, 5, 1)) >= 48 AND ASC(MID(UserID, 5, 1)) <= 57)
- THEN
- OUTPUT "Correct Format"
- ELSE
- OUTPUT "Wrong Format"
- ELSE
- OUTPUT "Wrong Format"
- '''
- # --------------------Task 2.2--------------------
- # Solution 1 (using Built-in function)
- print('--------------------Task 2.2--------------------')
- user_input = input("Please input user ID?")
- while True:
- if len(user_input) != 6:
- print("Invalid ID. It should have a length of 6")
- user_input = input("Please input user ID?")
- else:
- break
- # Validation
- if user_input[0].isupper() and user_input[1:3].islower() and user_input[3:].isnumeric():
- print("True. The input ID (%s) is valid." % user_input)
- else:
- print("False. The input ID (%s) is invalid." % user_input)
- # # Solution 2 (using RE)
- # import re
- # user_input = input("Please input user ID?")
- #
- # myRex = re.compile(r'^[A-Z][a-z]{2}\d\d\d$')
- #
- # mo = myRex.search(user_input)
- #
- # if mo == None:
- # print("False. The input ID (%s) is invalid." % user_input)
- # else:
- # print("True. The input ID (%s) is valid." % user_input)
- # --------------------Task 2.3--------------------
- print('--------------------Task 2.3--------------------')
- # Solution 1 (using Built-in function)
- user_input = input("Please input user ID?")
- while True:
- if len(user_input) != 6:
- print("Invalid ID. It should have a length of 6")
- user_input = input("Please input user ID?")
- else:
- break
- # Validation
- if user_input[:3].isalpha() and user_input[3:].isnumeric():
- print("True. The input ID (%s) is valid." % user_input)
- else:
- print("False. The input ID (%s) is invalid." % user_input)
- # # Solution 2 (using RE)
- # import re
- # user_input = input("Please input user ID?")
- #
- # myRex = re.compile(r'^[A-Za-z]{3}\d\d\d$')
- #
- # mo = myRex.search(user_input)
- #
- # if mo == None:
- # print("False. The input ID (%s) is invalid." % user_input)
- # else:
- # print("True. The input ID (%s) is valid." % user_input)
- # --------------------Task 2.4--------------------
- print('--------------------Task 2.4--------------------')
- # Solution 1 ONLY (using Built-in function)
- user_input = input("Please input user ID?")
- while True:
- if len(user_input) != 6:
- print("Invalid ID. It should have a length of 6")
- user_input = input("Please input user ID?")
- else:
- break
- # Validation Function
- def ValidateuserID(instring):
- if instring[:3].isalpha() and instring[3:].isnumeric():
- return True
- else:
- return False
- print("The validity of ID: " + str(ValidateuserID(user_input)))
Advertisement
Add Comment
Please, Sign In to add comment