SHARE
TWEET

CS-PreRelease_Task34.py

maincarry May 15th, 2016 28 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Task 3.4
  3. By Mark Wu
  4. @markwu.me
  5. '''
  6.  
  7. # import os
  8.  
  9.  
  10. # -----------------Welcome screen-----------------
  11. print("-----------------Task 3.4-----------------")
  12. print('''
  13.  
  14. -----Task 3 Overview-----
  15. [1] (Task 3.1) Create new records
  16. [2] (Task 3.2) See current records
  17. [3] (Task 3.3) Find member ID
  18. [4] (-----SELECTED-----) Add new users
  19. ''')
  20.  
  21.  
  22. # -----------------Task 3.4-----------------
  23.  
  24. # Initialization
  25. member_list = [] # List
  26.  
  27. # The following are the functions used in the program
  28.  
  29. def ValidateuserID(instring):
  30.     if len(instring) == 6 and instring[0].isupper() and instring[1:3].islower() and instring[3:].isnumeric():
  31.         return True
  32.     else:
  33.         return False
  34.  
  35.  
  36. def input_name():
  37.     instr = input("Please input member's name >>>") # string
  38.     while not 0 < len(instr) < 25 or instr in (','):
  39.         print("Invalid name. A valid name should be less than 25 characters. Please input again.")
  40.         instr = input("Please input member's name >>>")
  41.     return instr
  42.  
  43.  
  44. def input_id(member_name=''):
  45.     instr = input("Please input member ID for %s >>>" % member_name)
  46.     while not ValidateuserID(instr):
  47.         print("Invalid ID. Please try again.")
  48.         instr = input("Please input member ID for %s >>>" % member_name)
  49.     return instr
  50.  
  51. # Check if file exists:
  52. # file_found = os.path.isfile('record.txt') # Boolean
  53. file_found = True # Can't do this validation because I can't import
  54. if file_found:
  55.     try:
  56.         with open('record.txt') as myfile:
  57.             # following code to check the format of the existing file
  58.             content = myfile.readlines()
  59.             for each in content:
  60.                 each_member = each.split(',')
  61.                 each_member[1] = each_member[1].strip() # remove line breakers
  62.  
  63.         print("Existing file found. All new input will be added to the file.")
  64.     except:
  65.         # try:
  66.         #     os.remove('record.txt')
  67.         #     print("Invalid record format. Bad file removed.")
  68.         # except:
  69.         #     print("Invalid record format. But bad file removal failed!")
  70.         print("Invalid record format.")
  71.  
  72. # Ask user to input data of new members
  73. # Input member
  74. flag = True
  75. while flag:
  76.     mem_name = input_name() # string
  77.     mem_id = input_id(mem_name) # string
  78.     new_member = [mem_name, mem_id] # List
  79.     member_list.append(new_member)
  80.  
  81.     # Ask if the user want to continue
  82.     to_continue = input("Input 'stop' to save your progress, otherwise please enter anything to continue.")
  83.     if to_continue == 'stop':
  84.         flag = False
  85.  
  86. # Create File and store the result
  87. try:
  88.     with open('record.txt', mode='a') as myfile:
  89.         for each in member_list:
  90.             newline = ','.join(each)
  91.             myfile.write(newline + '\n')
  92.         print('Data successfully stored.')
  93. except:
  94.     print("Can't create or alter file! Please check file permission.")
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