Python 3.5 Need the following code changed so that it is just one function, and
ID: 3810435 • Letter: P
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()
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(" 1 - Read Class List 2 - List Students 3 - Exit ");
# Accepting user choice
sel = int(input(" 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();
Explanation / Answer
class Student:
def __init__(self,name = "",id = "",email=""):
self.id = id;
self.name = name;
self.email =email;
def read_classlist():
#fileName = input(" Enter file name: ");
students = [];
#inputfile = .join(fileName);
try:
fp = open("E:/Chegg/studentdata.txt", "r");
for line in fp:
line = line.strip();
data = line.split(",");
obj = Student(data[0], data[1], data[2]);
students.append(obj);
if len(students) == 0:
print(" There are no students to display... ");
else:
for stu in students:
print(" " + stu.id + ": " + stu.name + ", " + stu.email);
fp.close();
print(" File has been successfully read... ");
except IOError:
print(" File Doesn't Exist... ");
def main():
## s = Student();
## s.__init__("xyz",1,"abcmail.com");
read_classlist();
main();
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.