diff --git a/text-file-database.py b/text-file-database.py index daca8e9..17cb36d 100644 --- a/text-file-database.py +++ b/text-file-database.py @@ -1,5 +1,7 @@ +# math used for rounding down import math +# add new user's info to end of file def insert(lines): print("ID:", math.floor(len(lines)/3)) lines.append(input("Name: ") + " \n") @@ -7,6 +9,7 @@ def insert(lines): lines.append(input("School year: ") + " \n") return lines +# remove user's info from file, by user ID def delete(lines): id = input("ID: ") for i in range(math.floor(len(lines)/3)): @@ -16,6 +19,7 @@ def delete(lines): lines.pop(i*3) return lines +# print a user's info from file, by user ID def select(lines): id = input("ID: ") for i in range(math.floor(len(lines)/3)): @@ -24,6 +28,7 @@ def select(lines): else: print("ID not found") +# replace a user's info in file, by user ID def replace(lines): id = input("ID: ") for i in range(math.floor(len(lines)/3)): @@ -41,27 +46,40 @@ def replace(lines): lines.pop(i*3) return lines +# attempt to open file try: file = open("data.txt","r+") +# if file doesn't exist, create it except: file = open("data.txt","w+") +# read the lines from the file lines = file.readlines() +# run until user chooses to exit while True: + # print each user's info, one user per line for i in range(math.floor(len(lines)/3)): + # only print the content if the file isn't empty if len(lines) > 2: print("\n" + str(i) + " - name:", lines[i*3].strip(" \n") + ", age:", lines[i*3+1].strip(" \n") + ", school year:", lines[i*3+2].strip(" \n") + "\n") + # ask the user which function they want to perform function = input("Function (insert, i; delete, d; select, s; replace, r; exit, e): ") + # add user to file if function.strip(" ") == "i": lines = insert(lines) file.writelines(lines) + # remove user from file elif function.strip(" ") == "d": lines = delete(lines) file.writelines(lines) + # print user's info from file elif function.strip(" ") == "s": select(lines) + # replace user's info in file elif function.strip(" ") == "r": lines = replace(lines) file.writelines(lines) + # exit the program by breaking the loop elif function.strip(" ") == "e": break +# close the file file.close() \ No newline at end of file