maincarry

CS-PreRelease_Task33.py

May 15th, 2016
46
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Task 3.3
  3. By Mark Wu
  4. @markwu.me
  5. '''
  6.  
  7. # import os
  8.  
  9.  
  10. # -----------------Welcome screen-----------------
  11. print("-----------------Task 3.3-----------------")
  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] (-----SELECTED-----) Find member ID
  18. [4] (Task 3.4) Add new users
  19. ''')
  20.  
  21.  
  22. # -----------------Task 3.3-----------------
  23.  
  24. # Initialization
  25. member_list = [] # List
  26. search_result = '' # String
  27.  
  28. # The following are the functions used in the program
  29.  
  30. def search_by_name(instr):
  31.     global search_result
  32.     found = False # Boolean
  33.  
  34.     try:
  35.         for each in member_list:
  36.             if each[0].lower() == instr:
  37.                 found = True
  38.                 search_result = each
  39.  
  40.         return found
  41.     except:
  42.         print("Error occured during the search. File may be corrupted.")
  43.         return False
  44.  
  45. def input_name():
  46.     instr = input("Please input member's name (Search is not case sensitive) >>>") # string
  47.     while not 0 < len(instr) < 25 or instr in (','):
  48.         print("Invalid name. A valid name should be less than 25 characters. Please input again.")
  49.         instr = input("Please input member's name (Search is not case sensitive) >>>")
  50.     return instr.lower()
  51.  
  52. # Check if file exists:
  53. # file_found = os.path.isfile('record.txt') # Boolean
  54. file_found = True # Can't do this validation because I can't import
  55. if file_found:
  56.     try:
  57.         with open('record.txt') as myfile:
  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.                 member_list.append(each_member)
  63.     except:
  64.         print("Invalid record format.")
  65.     else:
  66.         # Ask for the name
  67.         in_name = input_name()
  68.         if search_by_name(in_name):
  69.             print("Member found! %s's member ID is %s." % (search_result[0], search_result[1]))
  70.         else:
  71.             print("No player named %s found." % in_name)
  72.  
  73. else:
  74.     print("File not exist!")
RAW Paste Data