SHARE
TWEET

CS-PreRelease_Task2.py

maincarry May 15th, 2016 (edited) 128 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Task 2
  3. by Mark Wu
  4.  
  5. Format: Ull000
  6.  
  7. Here's two different solutions to this question,
  8. using either Built-in functions or Regular Expression
  9. '''
  10.  
  11. # --------------------Task 2.1--------------------
  12. '''
  13. UserID <-- ''
  14. PRINT "Please input UserID:"
  15. INPUT UserID
  16. IF LENGTH(UserID) = 6 THEN
  17.    IF LEFT(UserID, 1) = UCASE(LEFT(UserID, 1)) AND
  18.       MID(UserID, 2, 2) = LCASE(MID(UserID, 2, 2)) AND
  19.       (ASC(RIGHT(UserID, 1)) >= 48 AND ASC(RIGHT(UserID, 1)) <= 57) AND
  20.       (ASC(MID(UserID, 4, 1)) >= 48 AND ASC(MID(UserID, 4, 1)) <= 57) AND
  21.       (ASC(MID(UserID, 5, 1)) >= 48 AND ASC(MID(UserID, 5, 1)) <= 57)
  22.    THEN
  23.        OUTPUT "Correct Format"
  24.    ELSE
  25.        OUTPUT "Wrong Format"
  26. ELSE
  27.    OUTPUT "Wrong Format"
  28. '''
  29.  
  30. # --------------------Task 2.2--------------------
  31. # Solution 1 (using Built-in function)
  32. print('--------------------Task 2.2--------------------')
  33. user_input = input("Please input user ID?")
  34.  
  35. while True:
  36.     if len(user_input) != 6:
  37.         print("Invalid ID. It should have a length of 6")
  38.         user_input = input("Please input user ID?")
  39.     else:
  40.         break
  41.  
  42. # Validation
  43. if user_input[0].isupper() and user_input[1:3].islower() and user_input[3:].isnumeric():
  44.     print("True. The input ID (%s) is valid." % user_input)
  45. else:
  46.     print("False. The input ID (%s) is invalid." % user_input)
  47.  
  48. # # Solution 2 (using RE)
  49. # import re
  50. # user_input = input("Please input user ID?")
  51. #
  52. # myRex = re.compile(r'^[A-Z][a-z]{2}\d\d\d$')
  53. #
  54. # mo = myRex.search(user_input)
  55. #
  56. # if mo == None:
  57. #     print("False. The input ID (%s) is invalid." % user_input)
  58. # else:
  59. #     print("True. The input ID (%s) is valid." % user_input)
  60.  
  61. # --------------------Task 2.3--------------------
  62. print('--------------------Task 2.3--------------------')
  63. # Solution 1 (using Built-in function)
  64. user_input = input("Please input user ID?")
  65.  
  66. while True:
  67.     if len(user_input) != 6:
  68.         print("Invalid ID. It should have a length of 6")
  69.         user_input = input("Please input user ID?")
  70.     else:
  71.         break
  72.  
  73. # Validation
  74. if user_input[:3].isalpha() and user_input[3:].isnumeric():
  75.     print("True. The input ID (%s) is valid." % user_input)
  76. else:
  77.     print("False. The input ID (%s) is invalid." % user_input)
  78.  
  79. # # Solution 2 (using RE)
  80. # import re
  81. # user_input = input("Please input user ID?")
  82. #
  83. # myRex = re.compile(r'^[A-Za-z]{3}\d\d\d$')
  84. #
  85. # mo = myRex.search(user_input)
  86. #
  87. # if mo == None:
  88. #     print("False. The input ID (%s) is invalid." % user_input)
  89. # else:
  90. #     print("True. The input ID (%s) is valid." % user_input)
  91.  
  92. # --------------------Task 2.4--------------------
  93. print('--------------------Task 2.4--------------------')
  94. # Solution 1 ONLY (using Built-in function)
  95. user_input = input("Please input user ID?")
  96.  
  97. while True:
  98.     if len(user_input) != 6:
  99.         print("Invalid ID. It should have a length of 6")
  100.         user_input = input("Please input user ID?")
  101.     else:
  102.         break
  103.  
  104.  
  105. # Validation Function
  106. def ValidateuserID(instring):
  107.     if instring[:3].isalpha() and instring[3:].isnumeric():
  108.         return True
  109.     else:
  110.         return False
  111.  
  112.  
  113. print("The validity of ID: " + str(ValidateuserID(user_input)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top