Question: Python 3.5 Need the following code changed so that it is just one function, and not a class with sub-functions. The function should be read_classlist()

Python 3.5

Need the following code changed so that it is just one function, and not a class with sub-functions.

The function should be read_classlist()

It reads the classlist from file, and prints a list like in printData(self) and displayRecords(students)

The menu function can be ignored completely.

class Student: """ Class definition """ def __init__(self, name="", id="", mail=""): """ Constructor """ self.name = name; self.id = id; self.mail = mail; self.marks = []; def printData(self): """ Class method that prints Student data """ print(" " + self.id + ": " + self.name + ", " + self.mail + ", marks: ", end=""); print(self.marks); def display_menu(): """ Function that displays menu """ # Printing menu print(" \t 1 - Read Class List \t 2 - List Students \t 3 - Exit "); # Accepting user choice sel = int(input(" \t Your selection: ")); # Return user selected value return sel; def read_classlist(): """ Function that reads file and generates a list of student objects """ # Reading file name fileName = input(" Enter file name: "); # List to hold student objects students = []; try: # Opening file in read mode fp = open(fileName, "r"); # Reading line by line for line in fp: # Stripping white space line = line.strip(); # Splitting data on comma data = line.split(","); # Constructing student object obj = Student(data[0], data[1], data[2]); # Storing object into list students.append(obj); # Closing file fp.close(); print(" File has been successfully read... "); except IOError: # Handling exceptions print(" File Doesn't Exist... "); finally: # Return list of students return students; def displayRecords(students): """ Function that displays Student data """ # If there are no students if len(students) == 0: print(" There are no students to display... "); else: # Iterating over student objects for stu in students: # Printing student data stu.printData(); def main(): """ Main function """ # Empty list students = []; # Iterate till user wants to quit while(True): # Reading selection by displaying menu sel = display_menu(); # Reading data if sel == 1: students = read_classlist(); # Displaying elif sel == 2: displayRecords(students); # Exit else: return; # Calling main function main();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!