Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Task 3.4
- By Mark Wu
- @markwu.me
- '''
- # import os
- # -----------------Welcome screen-----------------
- print("-----------------Task 3.4-----------------")
- print('''
- -----Task 3 Overview-----
- [1] (Task 3.1) Create new records
- [2] (Task 3.2) See current records
- [3] (Task 3.3) Find member ID
- [4] (-----SELECTED-----) Add new users
- ''')
- # -----------------Task 3.4-----------------
- # Initialization
- member_list = [] # List
- # The following are the functions used in the program
- def ValidateuserID(instring):
- if len(instring) == 6 and instring[0].isupper() and instring[1:3].islower() and instring[3:].isnumeric():
- return True
- else:
- return False
- def input_name():
- instr = input("Please input member's name >>>") # string
- while not 0 < len(instr) < 25 or instr in (','):
- print("Invalid name. A valid name should be less than 25 characters. Please input again.")
- instr = input("Please input member's name >>>")
- return instr
- def input_id(member_name=''):
- instr = input("Please input member ID for %s >>>" % member_name)
- while not ValidateuserID(instr):
- print("Invalid ID. Please try again.")
- instr = input("Please input member ID for %s >>>" % member_name)
- return instr
- # Check if file exists:
- # file_found = os.path.isfile('record.txt') # Boolean
- file_found = True # Can't do this validation because I can't import
- if file_found:
- try:
- with open('record.txt') as myfile:
- # following code to check the format of the existing file
- content = myfile.readlines()
- for each in content:
- each_member = each.split(',')
- each_member[1] = each_member[1].strip() # remove line breakers
- print("Existing file found. All new input will be added to the file.")
- except:
- # try:
- # os.remove('record.txt')
- # print("Invalid record format. Bad file removed.")
- # except:
- # print("Invalid record format. But bad file removal failed!")
- print("Invalid record format.")
- # Ask user to input data of new members
- # Input member
- flag = True
- while flag:
- mem_name = input_name() # string
- mem_id = input_id(mem_name) # string
- new_member = [mem_name, mem_id] # List
- member_list.append(new_member)
- # Ask if the user want to continue
- to_continue = input("Input 'stop' to save your progress, otherwise please enter anything to continue.")
- if to_continue == 'stop':
- flag = False
- # Create File and store the result
- try:
- with open('record.txt', mode='a') as myfile:
- for each in member_list:
- newline = ','.join(each)
- myfile.write(newline + '\n')
- print('Data successfully stored.')
- except:
- print("Can't create or alter file! Please check file permission.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement