This program needs to implement the following functions:
1) Add Student: This operation adds a new student entry to the 'students.txt' file. The process begins by prompting the user for the student's first name and last name. The system needs to check that the name contains only letters, and the names start with capital letters. Subsequently, the system automatically generates the email address in the format of firstname.lastname@lut.fi. Additionally, the system generates a unique study number by randomly selecting a number between 10000 and 99999, ensuring it's not already in use. The starting year is automatically set to the current year, utilizing the 'datetime' library to retrieve this information."
2) Search student: You can find any student by giving a part of the first name or a part of the last name of a student. The search string must contain at least three non-blank characters. This operation is suitable for finding the students ID, which is needed in operations 4 and 5. The operation prints the ID, the first and the last name according to the example below.
3) Search course: You can give a part of the course name or part of the teacher's name. Note that there can be several teachers in one course.
4) Add course completion: The system asks the required information step by step according to the following examples. If student has already passed the course, the system checks that the new grade is higher than the old grade. There can be at most one completion of one course by one student. The system needs to also check that the completion is added to the register within 30 days. It this is not true, then the program outputs: Input date is older than 30 days. Please contact "opinto." In such a case, nothing is added to the file.
5) Show student’s record: The program asks the students ID. Remember that you can search the ID by using operation 2. After this, the program displays the student’s information according to the model below:
Result:
Code:
import random import datetime def menu(): while 1: print('''You may select one of the following: 1) Add student 2) Search student 3) Search course 4) Add course completion 5) Show student's record 0) Exit''') selection=input("What is your selection?\n") if selection=="1": Add_student() print() continue elif selection=="2": Search_student() print() continue elif selection=="3": Search_course() print() continue elif selection=="4": Add_course_com() print() continue elif selection=="5": Show_student_re() print() continue elif selection=="0": break else: print("Try again,enter number 0--5 please.") print() continue def Add_student(): while True: print("Names should contain only letters and start with a capital letter.") First_name = input("Enter the first name of the student:\n") Last_name = input("Enter the last name of the student:\n") condition_firstname = 0 condition_lastname = 0 # Determine whether the condition for capitalizing the first letter of firstname is met if First_name[0].isupper(): # Check if all characters in the rest of the firstname are lowercase letters if First_name[1:].islower() and First_name[1:].isalpha(): condition_firstname = 1 # Determine whether the condition for capitalizing the first letter of lastname is met if Last_name[0].isupper(): # Check if all characters in the rest of the lastname are lowercase letters if Last_name[1:].islower() and Last_name[1:].isalpha(): condition_lastname = 1 # Meet all conditions if condition_firstname == 1 and condition_lastname == 1: break else: continue #Creat email email=First_name.lower()+"."+Last_name.lower()+"@lut.fi" #Selcet major while 1: print('''Select student's major: CE: Computational Engineering EE: Electrical Engineering ET: Energy Technology ME: Mechanical Engineering SE: Software Engineering''') major=input("What is your selection?\n") if major=="CE" or major=="EE" or major=="ET" or major=="ME" or major=="SE": print("Student added successfully!") break else: continue #Retrieve existing student ID and generate new unique student ID list_ID = [] with open("students.txt", "r") as fp: num_lines = len(fp.readlines()) fp.seek(0) #Reposition the file pointer to the beginning of the file for i in range(num_lines): ID = fp.readline().strip().split(",")[0] list_ID.append(ID) while 1: new_ID = random.randint(10000, 99999) if new_ID in list_ID: continue else: break #Get the current year current_datetime = datetime.datetime.now() current_year = current_datetime.year #Add information of new studnets with open("students.txt","a") as fp: content=str(new_ID)+","+First_name+","+Last_name+","+str(current_year)+","+major+","+email+"\n" fp.write(content) def Search_student(): while 1 : character=input("Give at least 3 characters of the students first or last name:\n") list_firstname=[] list_lastname=[] list_ID=[] if character[:].isalpha() and len(character)>=3: with open("students.txt","r") as fp: content=fp.readlines() num_lines = len(content) fp.seek(0) #Reposition the file pointer to the beginning of the file for i in range(num_lines): ID = fp.readline().strip().split(",")[0] list_ID.append(ID) fp.seek(0) for i in range(num_lines): firstname = fp.readline().strip().split(",")[1] list_firstname.append(firstname) fp.seek(0) for i in range(num_lines): lastname = fp.readline().strip().split(",")[2] list_lastname.append(lastname) n=[] for i in range(len(list_firstname)): if character in list_firstname[i]: n.append(i) k=[] for i in range(len(list_lastname)): if character in list_lastname[i]: k.append(i) #The input character cannot be detected if n==[] and k==[]: print("The student cannot be detected,Try again.\n") continue elif n!=[] and k==[]: print("Matching studnets:") for j in n: if 0<=j < num_lines: line_need=content[j].split(",") print("ID: ",line_need[0],", First name: ",line_need[1],", Last name: ",line_need[2],sep="") break elif n==[] and k!=[]: print("Matching studnets:") for j in k: if 0<=j < num_lines: line_need=content[j].split(",") print("ID: ",line_need[0],", First name: ",line_need[1],", Last name: ",line_need[2],sep="") break elif n!=[] and k!=[]: print("Matching studnets:") for j in n: if 0<=j < num_lines: line_need=content[j].split(",") print("ID: ",line_need[0],", First name: ",line_need[1],", Last name: ",line_need[2],sep="") for j in k: if 0<=j < num_lines: line_need=content[j].split(",") print("ID: ",line_need[0],", First name: ",line_need[1],", Last name: ",line_need[2],sep="") break else: continue return list_ID def Search_course(): while 1 : character=input("Give at least 3 characters of the name of the course or the teacher:\n") list_courses=[] list_teachers=[] list_ID=[] if character[:].isalpha() and len(character)>=3: with open("courses.txt","r") as fp: content=fp.readlines() num_lines = len(content) fp.seek(0) #Reposition the file pointer to the beginning of the file for i in range(num_lines): ID = fp.readline().strip().split(",")[0] list_ID.append(ID) fp.seek(0) for i in range(num_lines): courses_name = fp.readline().strip().split(",")[1] list_courses.append(courses_name) fp.seek(0) for i in range(num_lines): teachers_name = fp.readline().strip().split(",",4)[3] list_teachers.append(teachers_name) n=[] for i in range(len(list_courses)): if character in list_courses[i]: n.append(i) k=[] for i in range(len(list_teachers)): if character in list_teachers[i]: k.append(i) #The input character cannot be detected if n==[] and k==[]: print("The course cannot be detected,Try again.\n") continue if n!=[] and k==[]: for j in n: if 0<=j < num_lines: line_need=content[j].split(",") data_teachers=line_need[3:] teachers=', '.join(element.strip() for element in data_teachers) print("ID: ",line_need[0],", Name: ",line_need[1],", Teacher(s): ",teachers,sep="") break if n==[] and k!=[]: for j in k: if 0<=j < num_lines: line_need=content[j].split(",") data_teachers=line_need[3:] teachers=', '.join(element.strip() for element in data_teachers) print("ID: ",line_need[0],", Name: ",line_need[1],", Teacher(s): ",teachers,sep="") break if n!=[] and k!=[]: for j in n: if 0<=j < num_lines: line_need=content[j].split(",") data_teachers=line_need[3:] teachers=', '.join(element.strip() for element in data_teachers) print("ID: ",line_need[0],", Name: ",line_need[1],", Teacher(s): ",teachers,sep="") for j in k: if 0<=j < num_lines: line_need=content[j].split(",") data_teachers=line_need[3:] teachers=', '.join(element.strip() for element in data_teachers) print("ID: ",line_need[0],", Name: ",line_need[1],", Teacher(s): ",teachers,sep="") break else: continue def Add_course_com(): list_courses_ID=[] list_date=[] list_student_ID=[] list_passed_grade=[] with open("passed.txt","r") as fp: content=fp.readlines() num_lines = len(content) fp.seek(0) #Reposition the file pointer to the beginning of the file for i in range(num_lines): courses_ID = fp.readline().strip().split(",")[0] list_courses_ID.append(courses_ID) fp.seek(0) for i in range(num_lines): student_ID = fp.readline().strip().split(",")[1] list_student_ID.append(student_ID) fp.seek(0) for i in range(num_lines): passed_date = fp.readline().strip().split(",")[2] list_date.append(passed_date) fp.seek(0) for i in range(num_lines): passed_grade = fp.readline().strip().split(",")[3] list_passed_grade.append(passed_grade) check_courses_ID=input("Give the course ID:\n") while check_courses_ID not in list_courses_ID: check_courses_ID=input("Give the course ID:\n") check_student_ID=input("Give the student ID:\n") while check_student_ID not in list_student_ID: check_student_ID=input("Give the student ID:\n") grade=input("Give the grade:\n") if grade=="1"or grade=="2" or grade=="3"or grade=="4" or grade=="5": n=[] for i in range(len(list_courses_ID)): if check_courses_ID == list_courses_ID[i]: n.append(i) k=[] for j in range(len(list_student_ID)): if check_student_ID == list_student_ID[j]: k.append(j) need_lines_th=[] for a in n: for b in k: if a==b: need_lines_th.append(a) if need_lines_th==[]: try: t = input("Enter a date (DD/MM/YYYY):\n") new_date = datetime.datetime.strptime(t, "%d/%m/%Y") current_time = datetime.datetime.now() if (current_time-new_date).days>30: print("Input date is older than 30 days. Contact \"opinto\".") elif 30>=(current_time-new_date).days>=0: with open("passed.txt","a") as fp: content=check_courses_ID+","+check_student_ID+","+t+","+grade+"\n" fp.write(content) print("Input date is valid.") print("Record added!") else: print("Input date is later than today. Try again!") except ValueError: print("Invalid date format. Use DD/MM/YYYY. Try again!") else: for num in need_lines_th: if list_passed_grade [num]>= grade: print("Student has passed this course earlier with grade",list_passed_grade [num]) else: try: t = input("Enter a date (DD/MM/YYYY):\n") new_date = datetime.datetime.strptime(t, "%d/%m/%Y") current_time = datetime.datetime.now() if (current_time-new_date).days>30: print("Input date is older than 30 days. Contact \"opinto\".") elif 30>=(current_time-new_date).days>=0: del content[need_lines_th[0]] with open("passed.txt", 'w') as file: file.writelines(content) with open("passed.txt","a") as fp: content=check_courses_ID+","+check_student_ID+","+t+","+grade+"\n" fp.write(content) print("Input date is valid.") print("Record added!") break else: print("Input date is later than today. Try again!") except ValueError: print("Invalid date format. Use DD/MM/YYYY. Try again!") else: print("Grade is not a correct grade.") def Show_student_re(): need_lines_s=[] need_lines_p=[] need_lines_c=[] list_ID_s=[] list_ID_p=[] list_ID_c=[] with open("students.txt","r") as fp: content_s=fp.readlines() num_lines_s = len(content_s) fp.seek(0) #Reposition the file pointer to the beginning of the file for i in range(num_lines_s): ID_s = fp.readline().strip().split(",")[0] list_ID_s.append(ID_s) with open("passed.txt","r") as fp: content_p=fp.readlines() num_lines_p = len(content_p) fp.seek(0) for i in range(num_lines_p): ID_p = fp.readline().strip().split(",")[1] list_ID_p.append(ID_p) with open("courses.txt","r") as fp: content_c=fp.readlines() num_lines_c = len(content_c) fp.seek(0) for i in range(num_lines_c): ID_c = fp.readline().strip().split(",")[0] list_ID_c.append(ID_c) student_ID=input("Student ID: ") if student_ID in list_ID_s: for i in range(len(list_ID_s)): if student_ID == list_ID_s[i]: need_lines_s.append(i) s=need_lines_s[0] student_data = content_s[s].strip().split(",") print("Name: ",student_data[2],", ",student_data[1],sep="") print("Starting year:",student_data[3]) if student_data[4]=="CE": print("Major: Computational Engineering") elif student_data[4]=="EE": print("Major: Electrical Engineering") elif student_data[4]=="ET": print("Major: Energy Technology") elif student_data[4]=="ME": print("Major: Mechanical Engineering") elif student_data[4]=="SE": print("Major: Software Engineering") print("Email:",student_data[5],"\n") print("Passed courses:\n") credit_total=0 grade_total=0 for i in range(len(list_ID_p)): if student_ID == list_ID_p[i]: need_lines_p.append(i) for i in need_lines_p: passed_data = content_p[i].strip().split(",") Courses_ID=passed_data[0] Date=passed_data[2] grade=passed_data[3] for j in range(len(list_ID_c)): if Courses_ID == list_ID_c[j]: need_lines_c.append(j) for k in need_lines_c: course_data = content_c[k].strip().split(",") Course_name=course_data[1] Credits=course_data[2] credit_total+=int(Credits) grade_total+=int(grade) for i in need_lines_p: average=grade_total/len(need_lines_p) data_teachers=course_data[3:] teachers=', '.join(element.strip() for element in data_teachers) print("Course ID: ",Courses_ID,", Name: ",Course_name,", Credits: ",Credits,"\n""Date: ",Date,", Teacher(s): ",teachers,", grade: ",grade,"\n",sep="") print("Total credits: ",credit_total,", average grade: ",average,sep="") else : print("Wrong student ID!") menu()