Last active
December 26, 2022 18:46
-
-
Save Hypro999/d6e6473addb1e5ce2aafed72a9bbbd33 to your computer and use it in GitHub Desktop.
The data is in the form of a list which subsequently contains dictionaries - code snippet for saving and loading. The idea for PyStudentManager was inspired by Bo Milanovich in his Pluralsight course. Note: I made this a long while ago when I was new to programming (in general, but more specifically in Python) which is why the code may cause you…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
students = [] | |
# Titlecasing: | |
def get_students_tica(): | |
students_tica = [] | |
i = 0 | |
while i < len(students): | |
x = {'name': students[i]['name'].title(), 'id': students[i]['id']} | |
students_tica.append(x) | |
i += 1 | |
return students_tica | |
def print_students_tica(): | |
if len(students) != 0: | |
print(get_students_tica()) | |
elif len(students) == 0: | |
print('No students entered yet') | |
else: | |
print('oops') | |
#Adding students and initial prompt: | |
def add_student(name, student_id=0): | |
student = {'name': name, 'id': student_id} | |
students.append(student) | |
def prompt(): | |
try: | |
q1 = input('Add a new student?(y/n)') | |
if q1 == 'y': | |
q2 = input('Enter Student Name: ') | |
q3 = input('Enter Student Id: ') | |
add_student(name=q2, student_id=int(q3)) | |
prompt() | |
elif q1 == 'n': | |
print('your students: ') | |
print_students_tica() | |
else: | |
print('answer must be y/n') | |
prompt() | |
except TypeError as te: | |
print('you entered the wrong type of character!') | |
print(te) | |
except (Exception != TypeError) as err: | |
print('an error has occurred!!!') | |
print(err) | |
# Saving, Loading and Clearing functions: | |
def save(): | |
with open('Data.txt', 'w') as f: | |
i = 0 | |
while i < len(students): | |
student_name = students[i]['name'].title() | |
student_id = students[i]['id'] | |
statement = student_name + "," + str(student_id) | |
f.write(statement + ',') | |
i += 1 | |
f.close() | |
def load(): | |
with open("Data.txt", 'r') as f: | |
for line in f.readlines(): | |
lt = line.split(',') | |
i = 0 | |
while i < (len(lt)-1): | |
ltn = lt[i] | |
ltid = lt[i + 1] | |
add_student(name=ltn, student_id=int(ltid)) | |
i += 2 | |
if __name__ == "__main__": | |
load() | |
prompt() | |
save() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment