maincarry

CS-PreRelease_Task35.py

May 15th, 2016
49
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Task 3.5
  3. By Mark Wu
  4. @markwu.me
  5. '''
  6.  
  7. # import os
  8. # import re
  9. # import datetime
  10. # datetime is a good module to use to check date, but for demonstration purpose this is not used.
  11.  
  12. # Initialization
  13. member_list = [] # List
  14.  
  15. # The following are the functions used in the program
  16.  
  17. def validateuserID(instring):
  18.     if len(instring) == 6 and instring[0].isupper() and instring[1:3].islower() and instring[3:].isnumeric():
  19.         return True
  20.     else:
  21.         return False
  22.  
  23. def validateDate(instring = ''):
  24.     # input format: MMDDYYYY
  25.     if len(instring) == 8 and instring.isnumeric():
  26.         month = int(instring[:2])
  27.         day = int(instring[2:4])
  28.         year = int(instring[-4:])
  29.         if 0 <= month <= 12 and 0 <= day <= 31 and 1800 <= year <= 2016:
  30.             formatted_date = '%i-%i-%i' % (month, day, year)
  31.             return formatted_date
  32.     return ''
  33.  
  34.  
  35. def input_name():
  36.     instr = input("Please input member's name >>>") # string
  37.     while not 0 < len(instr) < 25 or instr in (','):
  38.         print("Invalid name. A valid name should be less than 25 characters. Please input again.")
  39.         instr = input("Please input member's name >>>")
  40.     return instr
  41.  
  42.  
  43. def input_id(member_name=''):
  44.     instr = input("Please input member ID for %s >>>" % member_name)
  45.     while not validateuserID(instr):
  46.         print("Invalid ID. Please try again.")
  47.         instr = input("Please input member ID for %s >>>" % member_name)
  48.     return instr
  49.  
  50.  
  51. def input_phone(member_name=''):
  52.     instr = input("Please input the phone number for %s (11 digits) >>>" % member_name)
  53.     while not instr.isnumeric() or len(instr) != 11:
  54.         print("Invalid Phone Number (Should be 11 digits). Please try again.")
  55.         instr = input("Please input the phone number for %s (11 digits) >>>" % member_name)
  56.     return instr
  57.  
  58. def input_date(member_name=''):
  59.     instr = input("Please input the membership start date for %s (MM-DD-YYYY) >>>" % member_name)
  60.  
  61.     result = validateDate(instr) # blank string acts as False
  62.     while not result:
  63.         print("Invalid membership start date (MM-DD-YYYY).")
  64.         instr = input("Please input the membership start date for %s (MM-DD-YYYY) >>>" % member_name)
  65.         result = validateDate(instr)
  66.     return result
  67.  
  68.  
  69. # Check if file exists:
  70. # file_found = os.path.isfile('record.txt') # Boolean
  71. file_found = True # Can't do this validation because I can't import
  72.  
  73. if file_found:
  74.     try:
  75.         with open('record.txt') as myfile:
  76.             # following code to check the format of the existing file
  77.             content = myfile.readlines()
  78.             for each in content:
  79.                 each_member = each.split(',')
  80.                 each_member[1] = each_member[1].strip() # remove line breakers
  81.                 member_list.append(each_member)
  82.         print("Existing file found. All new input will be added to the file.")
  83.  
  84.     except:
  85.         # # remove bad files
  86.         # try:
  87.         #     os.remove('record.txt')
  88.         #     print("Invalid record format. Bad file removed.")
  89.         # except:
  90.         #     print("Invalid record format. But bad file removal failed!")
  91.         # finally:
  92.         print("Due to inconsistent file, program is aborted")
  93.         exit()
  94.  
  95. # Ask user to input data for each members
  96. for each in member_list:
  97.     member_name = each[0]
  98.     print("\nNow please add data for %s." % member_name)
  99.     each.append(input_phone(member_name))
  100.     each.append(input_date(member_name))
  101.  
  102. # Create New File and store the result
  103. try:
  104.     with open('record_appended.txt', mode='w') as myfile:
  105.         for each in member_list:
  106.             newline = ','.join(each)
  107.             myfile.write(newline + '\n')
  108.         print('Data successfully stored.')
  109. except:
  110.     print("Can't create file! Please check file permission.")
RAW Paste Data