Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions