Add comments
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
|
# math used for rounding down
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
# add new user's info to end of file
|
||||||
def insert(lines):
|
def insert(lines):
|
||||||
print("ID:", math.floor(len(lines)/3))
|
print("ID:", math.floor(len(lines)/3))
|
||||||
lines.append(input("Name: ") + " \n")
|
lines.append(input("Name: ") + " \n")
|
||||||
@@ -7,6 +9,7 @@ def insert(lines):
|
|||||||
lines.append(input("School year: ") + " \n")
|
lines.append(input("School year: ") + " \n")
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
# remove user's info from file, by user ID
|
||||||
def delete(lines):
|
def delete(lines):
|
||||||
id = input("ID: ")
|
id = input("ID: ")
|
||||||
for i in range(math.floor(len(lines)/3)):
|
for i in range(math.floor(len(lines)/3)):
|
||||||
@@ -16,6 +19,7 @@ def delete(lines):
|
|||||||
lines.pop(i*3)
|
lines.pop(i*3)
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
# print a user's info from file, by user ID
|
||||||
def select(lines):
|
def select(lines):
|
||||||
id = input("ID: ")
|
id = input("ID: ")
|
||||||
for i in range(math.floor(len(lines)/3)):
|
for i in range(math.floor(len(lines)/3)):
|
||||||
@@ -24,6 +28,7 @@ def select(lines):
|
|||||||
else:
|
else:
|
||||||
print("ID not found")
|
print("ID not found")
|
||||||
|
|
||||||
|
# replace a user's info in file, by user ID
|
||||||
def replace(lines):
|
def replace(lines):
|
||||||
id = input("ID: ")
|
id = input("ID: ")
|
||||||
for i in range(math.floor(len(lines)/3)):
|
for i in range(math.floor(len(lines)/3)):
|
||||||
@@ -41,27 +46,40 @@ def replace(lines):
|
|||||||
lines.pop(i*3)
|
lines.pop(i*3)
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
# attempt to open file
|
||||||
try:
|
try:
|
||||||
file = open("data.txt","r+")
|
file = open("data.txt","r+")
|
||||||
|
# if file doesn't exist, create it
|
||||||
except:
|
except:
|
||||||
file = open("data.txt","w+")
|
file = open("data.txt","w+")
|
||||||
|
# read the lines from the file
|
||||||
lines = file.readlines()
|
lines = file.readlines()
|
||||||
|
# run until user chooses to exit
|
||||||
while True:
|
while True:
|
||||||
|
# print each user's info, one user per line
|
||||||
for i in range(math.floor(len(lines)/3)):
|
for i in range(math.floor(len(lines)/3)):
|
||||||
|
# only print the content if the file isn't empty
|
||||||
if len(lines) > 2:
|
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")
|
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): ")
|
function = input("Function (insert, i; delete, d; select, s; replace, r; exit, e): ")
|
||||||
|
# add user to file
|
||||||
if function.strip(" ") == "i":
|
if function.strip(" ") == "i":
|
||||||
lines = insert(lines)
|
lines = insert(lines)
|
||||||
file.writelines(lines)
|
file.writelines(lines)
|
||||||
|
# remove user from file
|
||||||
elif function.strip(" ") == "d":
|
elif function.strip(" ") == "d":
|
||||||
lines = delete(lines)
|
lines = delete(lines)
|
||||||
file.writelines(lines)
|
file.writelines(lines)
|
||||||
|
# print user's info from file
|
||||||
elif function.strip(" ") == "s":
|
elif function.strip(" ") == "s":
|
||||||
select(lines)
|
select(lines)
|
||||||
|
# replace user's info in file
|
||||||
elif function.strip(" ") == "r":
|
elif function.strip(" ") == "r":
|
||||||
lines = replace(lines)
|
lines = replace(lines)
|
||||||
file.writelines(lines)
|
file.writelines(lines)
|
||||||
|
# exit the program by breaking the loop
|
||||||
elif function.strip(" ") == "e":
|
elif function.strip(" ") == "e":
|
||||||
break
|
break
|
||||||
|
# close the file
|
||||||
file.close()
|
file.close()
|
||||||
Reference in New Issue
Block a user