maincarry

CS-PreRelease_Task31.py

May 15th, 2016
170
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Task 3.1
  3. By Mark Wu
  4. @markwu.me
  5. '''
  6.  
  7. # -----------------Welcome screen-----------------
  8. print("-----------------Task 3.1-----------------")
  9. print('''
  10.  
  11. -----Task 3 Overview-----
  12. [1] (-----SELECTED-----) Create new records
  13. [2] (Task 3.2) Check current records
  14. [3] (Task 3.3) Find member ID
  15. [4] (Task 3.4) Add new users
  16. ''')
  17.  
  18.  
  19. # u_choice = input("Which function do you want to choose? Please input its No. >>>")
  20. # while u_choice not in ['1','2','3','4']:
  21. #     print("Invalid choice. A valid choice would be 1, 2, 3 or 4.")
  22. #     u_choice = input("Which function do you want to choose? Please input its No. >>>")
  23. # if u_choice == '1':
  24.  
  25.  
  26.  
  27. # -----------------Task 3.1-----------------
  28. # The following are the functions used in the program
  29.  
  30. # Validation Function from Task 2.2
  31. def ValidateuserID(instring):
  32.     if len(instring) == 6 and instring[0].isupper() and instring[1:3].islower() and instring[3:].isnumeric():
  33.         return True
  34.     else:
  35.         return False
  36.  
  37. def input_name():
  38.     instr = input("Please input member's name >>>") # string
  39.     while not 0 < len(instr) < 25 or instr in (','):
  40.         print("Invalid name. A valid name should be less than 25 characters. Please input again.")
  41.         instr = input("Please input member's name >>>")
  42.     return instr
  43.  
  44.  
  45. def input_id(member_name=''):
  46.     instr = input("Please input member ID for %s >>>" % member_name)
  47.     while not ValidateuserID(instr):
  48.         print("Invalid ID. Please try again.")
  49.         instr = input("Please input member ID for %s >>>" % member_name)
  50.     return instr
  51.  
  52.  
  53. # Initialization
  54. member_list = [] # List
  55.  
  56. # Input member
  57. flag = True
  58. while flag:
  59.     mem_name = input_name() # String
  60.     mem_id = input_id(mem_name) # String
  61.     new_member = [mem_name, mem_id] # List
  62.     member_list.append(new_member)
  63.  
  64.     # Ask if the user want to continue
  65.     to_continue = input("Input 'stop' to save your progress, otherwise please enter anything to continue.")
  66.     if to_continue == 'stop':
  67.         flag = False
  68.  
  69. # Create File and store the result
  70. try:
  71.     with open('record.txt', mode='w') as myfile:
  72.         for each in member_list:
  73.             newline = ','.join(each)
  74.             myfile.write(newline + '\n')
  75.         print('Data successfully stored.')
  76. except:
  77.     print("Can't create file! Please check file permission.")
RAW Paste Data