Step 1: In this assignment, you will modify the student information program described above in the...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Step 1: In this assignment, you will modify the student information program described above in the module note "Work with Classes and Files." Before we start, please make sure you have followed the steps in the module note (Class and File section) and created a program named student_file.py. Test the program to ensure that you can create and save student information. You should be able to store student names in a file (your file location might be different from mine). The content of the file looks like this: record - Notepad File Edit Format Vi John Mary Joe dassn.isFunctia Array(d), k=new Array eturn n.ready.promise Wandlers&(n(d).trigger function K(){(d.ad record - Notepad File Edit Format View Help John, 001, john@excelsior.edu Mary, 002, mary@excelsior.edu Joe, 003, joe@excelsior.edu Event("onload", K); n(1))break;L.ow border:0;width:0;heig c.style.zoom-1)),c. <1: bb 0:"false" "string" typeof b) CamelCarethis In this assignment you will modify the program such that, in addition to storing the student name, the program will also store the student id and student email to the file. The new file should look like this: **Before proceeding to the next step, please delete the old student record file. Step 2: These are the two classes you've created: Student and StudentInfo. Additionally, there is a function named "read_file()." A. Modify the Student class. We will have three instance attributes for every Student instance. In addition to the existing self.name attribute, create two additional attributes: self.sid and self.email. Make sure that in .__init__() you also receive student id and email arguments and assign them to the corresponding instance attributes. Modify the print() function inside the Student class such that it not only prints out student name, but also student id and student email: print (f'A new student object: {self.name): (self.sid}: {self.email) is created.') B. Modify the while True loop. In line 48 of the original code: Insert Here 44 while True: # keeps running until user presses 0 45 46 47 48 49 50 Insert two input() functions to request the student's ID and email. Save them to two new variables (just like what we did in line 45). Mind the indentation level. The two input() functions should be in the same indentation level as line 45, which means they are NOT part of the if block. name = input('Please enter the student\'s name. Press 0 to quit: ') if name== '0': break C. In line 49 of the original code, in addition to just passing the student name, pass two additional variables - student id and student email that you received from the user during step B above. Eventually, the values of id and email will be assigned to the sid and email instance attributes (see step A above) of every student object. stu = Student (name) # a new Student object stu_info.add_student (stu) D. Modify the .add_student() method inside the StudentInfo class: 20 21 14 16 Instead of just appending the value of the student.name attribute to student_list, append the student object itself to the list: self.student_list.append(stu). That is, we append the student object itself, which includes student name, id, and email as its instance attributes, to the list. In the original program, student_list stores student names like this: ['John', 'Mary', 'Joe' ] In the modified code, each element in the list will be a student object. Modify Here def add student (self, stu): E. Modify the .save_student() method inside the StudentInfo class: self.student_list.append(stu.name) def save student (self): ***this method saves the content of record_file to dicontent""" #your path might be different from mine record_filepathlib. Path ('e: /record. txt') with record_file.open (mode='a', encoding='utf-8') as file: #open the file object in append mode for info in self.student_list: each "info" is a student name file.write(info+' ') In line 17 of the original code, we used a for loop to retrieve the elements stored in the student_list instance attribute. In each iteration, an element, according to line 21 of the "original" code in step D above, is a student name (stu.name). In step D we modified the code. Instead of storing student names in the list, we store student objects. That is, for each iteration of the for loop, a student object is retrieved and stored in the info variable (you don't need to modify line 17). To store student name, id, and email in the file, we modify line 18 as: That is, for each iteration of the for loop, a student object is retrieved and stored in the info variable (you don't need to modify line 17). To store student name, id, and email in the file, we modify line 18 as: file.write(info.name + info.sid + ',' + info.email+' ') F. In the new code you can see that the name, id, and email of a given student are separated by a comma (,). This student information is concatenated together (the plus (+) operator) and stored in the same line in the file. G. Modify the read_file() function. read_file() is a stand-alone function and does not belong to any class. It will be called when the program starts to read the content of record.txt and print it to the screen. |41| read_file() Please change lines 35 and 36 below: 28 def read_file(): 29 30 31 32 33 34 35 36 37 38 """this method loads saved student info from disk*** record_filepathlib. Path ('e: /record.txt') if record_file.exists(): else: Modify Here #your path might be different from mine print('Saved students record: ') with record_file.open (mode='r') as file: # open the file object in read mode for stu_name in file.readlines (): print (stu_name, end=) print('No saved student record') to: for stu_info in file.readlines (): stu_list=stu_info.split (',') #split each line by comma and save to a list print (f' (stu_list [0]): (stu_list [1]): (stu_list [2]}', end='') The for loop retrieves one line from the file at a time. Each line in the file is a text string of student information separated by commas (for example: 'John,001,john@excelsior.edu'). The .split() method is used to break the line (for each comma (.) as specified in the argument) into different parts and assign them as the elements of a list stu_list. (for example: ['John"001,john@excelsior.edu']). Then, each element is printed out through the print() function. H. Now, execute your new code (remember to delete the old record.txt first). This is a sample output when running for the first time: No saved student record Please enter the student's name. Press 0 to quit: John Please enter the student's student id: 001 Please enter the student's email: john@excelsior.edu A new student object: John: 001:john@excelsior.edu is created. separated by commas (for example: 'John,001,john@excelsior.edu). The .split() method is used to break the line (for each comma (.) as specified in the argument) into different parts and assign them as the elements of a list stu_list. (for example: [John",001"john@excelsior.edu']). Then, each element is printed out through the print() function. H. Now, execute your new code (remember to delete the old record.txt first). This is a sample output when running for the first time: No saved student record Please enter the student's name. Press 0 to quit: John Please enter the student's student id: 001 Please enter the student's email: john@excelsior.edu A new student object: John: 001:john@excelsior.edu is created. Please enter the student's name. Press 0 to quit: Mary Please enter the student's student id: 002 Please enter the student's email: mary@excelsior.edu A new student object: Mary: 002:mary@excelsior.edu is created. Please enter the student's name. Press 0 to quit: Joe Please enter the student's student id: 003 Please enter the student's email: joe@excelsior.edu A new student object: Joe: 003: joe@excelsior.edu is created. Please enter the student's name. Press 0 to quit: 0 The student information you entered will be saved to a file when you press 'O'. In the subsequent executions, the program will read and print the content of the record file first: Saved students record: John:001:john@excelsior.edu Mary:002: mary@excelsior.edu Joe: 003:joe@excelsior.edu Please enter the student's name. Press 0 to quit: Abby Please enter the student's student id: 004 Please enter the student's email: abby@excelsior.edu A new student object: Abby: 004: abby@excelsior.edu is created. Please enter the student's name. Press 0 to quit: 0 Step 1: In this assignment, you will modify the student information program described above in the module note "Work with Classes and Files." Before we start, please make sure you have followed the steps in the module note (Class and File section) and created a program named student_file.py. Test the program to ensure that you can create and save student information. You should be able to store student names in a file (your file location might be different from mine). The content of the file looks like this: record - Notepad File Edit Format Vi John Mary Joe dassn.isFunctia Array(d), k=new Array eturn n.ready.promise Wandlers&(n(d).trigger function K(){(d.ad record - Notepad File Edit Format View Help John, 001, john@excelsior.edu Mary, 002, mary@excelsior.edu Joe, 003, joe@excelsior.edu Event("onload", K); n(1))break;L.ow border:0;width:0;heig c.style.zoom-1)),c. <1: bb 0:"false" "string" typeof b) CamelCarethis In this assignment you will modify the program such that, in addition to storing the student name, the program will also store the student id and student email to the file. The new file should look like this: **Before proceeding to the next step, please delete the old student record file. Step 2: These are the two classes you've created: Student and StudentInfo. Additionally, there is a function named "read_file()." A. Modify the Student class. We will have three instance attributes for every Student instance. In addition to the existing self.name attribute, create two additional attributes: self.sid and self.email. Make sure that in .__init__() you also receive student id and email arguments and assign them to the corresponding instance attributes. Modify the print() function inside the Student class such that it not only prints out student name, but also student id and student email: print (f'A new student object: {self.name): (self.sid}: {self.email) is created.') B. Modify the while True loop. In line 48 of the original code: Insert Here 44 while True: # keeps running until user presses 0 45 46 47 48 49 50 Insert two input() functions to request the student's ID and email. Save them to two new variables (just like what we did in line 45). Mind the indentation level. The two input() functions should be in the same indentation level as line 45, which means they are NOT part of the if block. name = input('Please enter the student\'s name. Press 0 to quit: ') if name== '0': break C. In line 49 of the original code, in addition to just passing the student name, pass two additional variables - student id and student email that you received from the user during step B above. Eventually, the values of id and email will be assigned to the sid and email instance attributes (see step A above) of every student object. stu = Student (name) # a new Student object stu_info.add_student (stu) D. Modify the .add_student() method inside the StudentInfo class: 20 21 14 16 Instead of just appending the value of the student.name attribute to student_list, append the student object itself to the list: self.student_list.append(stu). That is, we append the student object itself, which includes student name, id, and email as its instance attributes, to the list. In the original program, student_list stores student names like this: ['John', 'Mary', 'Joe' ] In the modified code, each element in the list will be a student object. Modify Here def add student (self, stu): E. Modify the .save_student() method inside the StudentInfo class: self.student_list.append(stu.name) def save student (self): ***this method saves the content of record_file to dicontent""" #your path might be different from mine record_filepathlib. Path ('e: /record. txt') with record_file.open (mode='a', encoding='utf-8') as file: #open the file object in append mode for info in self.student_list: each "info" is a student name file.write(info+' ') In line 17 of the original code, we used a for loop to retrieve the elements stored in the student_list instance attribute. In each iteration, an element, according to line 21 of the "original" code in step D above, is a student name (stu.name). In step D we modified the code. Instead of storing student names in the list, we store student objects. That is, for each iteration of the for loop, a student object is retrieved and stored in the info variable (you don't need to modify line 17). To store student name, id, and email in the file, we modify line 18 as: That is, for each iteration of the for loop, a student object is retrieved and stored in the info variable (you don't need to modify line 17). To store student name, id, and email in the file, we modify line 18 as: file.write(info.name + info.sid + ',' + info.email+' ') F. In the new code you can see that the name, id, and email of a given student are separated by a comma (,). This student information is concatenated together (the plus (+) operator) and stored in the same line in the file. G. Modify the read_file() function. read_file() is a stand-alone function and does not belong to any class. It will be called when the program starts to read the content of record.txt and print it to the screen. |41| read_file() Please change lines 35 and 36 below: 28 def read_file(): 29 30 31 32 33 34 35 36 37 38 """this method loads saved student info from disk*** record_filepathlib. Path ('e: /record.txt') if record_file.exists(): else: Modify Here #your path might be different from mine print('Saved students record: ') with record_file.open (mode='r') as file: # open the file object in read mode for stu_name in file.readlines (): print (stu_name, end=) print('No saved student record') to: for stu_info in file.readlines (): stu_list=stu_info.split (',') #split each line by comma and save to a list print (f' (stu_list [0]): (stu_list [1]): (stu_list [2]}', end='') The for loop retrieves one line from the file at a time. Each line in the file is a text string of student information separated by commas (for example: 'John,001,john@excelsior.edu'). The .split() method is used to break the line (for each comma (.) as specified in the argument) into different parts and assign them as the elements of a list stu_list. (for example: ['John"001,john@excelsior.edu']). Then, each element is printed out through the print() function. H. Now, execute your new code (remember to delete the old record.txt first). This is a sample output when running for the first time: No saved student record Please enter the student's name. Press 0 to quit: John Please enter the student's student id: 001 Please enter the student's email: john@excelsior.edu A new student object: John: 001:john@excelsior.edu is created. separated by commas (for example: 'John,001,john@excelsior.edu). The .split() method is used to break the line (for each comma (.) as specified in the argument) into different parts and assign them as the elements of a list stu_list. (for example: [John",001"john@excelsior.edu']). Then, each element is printed out through the print() function. H. Now, execute your new code (remember to delete the old record.txt first). This is a sample output when running for the first time: No saved student record Please enter the student's name. Press 0 to quit: John Please enter the student's student id: 001 Please enter the student's email: john@excelsior.edu A new student object: John: 001:john@excelsior.edu is created. Please enter the student's name. Press 0 to quit: Mary Please enter the student's student id: 002 Please enter the student's email: mary@excelsior.edu A new student object: Mary: 002:mary@excelsior.edu is created. Please enter the student's name. Press 0 to quit: Joe Please enter the student's student id: 003 Please enter the student's email: joe@excelsior.edu A new student object: Joe: 003: joe@excelsior.edu is created. Please enter the student's name. Press 0 to quit: 0 The student information you entered will be saved to a file when you press 'O'. In the subsequent executions, the program will read and print the content of the record file first: Saved students record: John:001:john@excelsior.edu Mary:002: mary@excelsior.edu Joe: 003:joe@excelsior.edu Please enter the student's name. Press 0 to quit: Abby Please enter the student's student id: 004 Please enter the student's email: abby@excelsior.edu A new student object: Abby: 004: abby@excelsior.edu is created. Please enter the student's name. Press 0 to quit: 0
Expert Answer:
Answer rating: 100% (QA)
Detailed instructions on how to modify the existing student information program to include student IDs and email addresses Based on your instructions ... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Given the following function int f (int x, int y, int z){ int p = z, a =0; while (x!=0){ if (x >y){ a+=x--; if (xy){ p*=a+x; x=0;} } } } } else{ p*=y; if (x>0) x else x++; return p; A) What does the...
-
Mr Ha is interested in setting up a coffee shop in Canberra, Australia. After consulting with his brother who owns a caf in other town of the territory, he has decided to commence the business in...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
If the resultant force acting on the hook is F R = {? 200i + 800j + 150k} lb, determine the magnitude and coordinate direction angles of F. 30 F= 600 ib
-
In Table 7.2 the drag coefficient of a 4:1 elliptical cylinder in laminar-boundary layer flow is 0.35. According to Patton [17], the hydrodynamic mass of this cylinder is πρ hb/4, where b is...
-
If A, B, C, and D are languages over , prove that (a) (A B ^ C D) 4 AC BD and (b) A = A = .
-
Give the advantages and disadvantages of petrol and diesel engines.
-
Madison Park Co-op, a whole foods grocery and gift shop, has provided the following data to be used in its service department cost allocations: Required: Using the step-down method, allocate the...
-
Smith Electronic Company's chip-mounting production department had 300 units of unfinished product, each 50% completed on September 30. During October of the same year, this department put another...
-
Exhibit 4 in the case shows the average cost per passenger for British Airways. Classify the listed operating expenses per passenger as fixed or variable. (Note: Assume aircraft capacity is fixed....
-
Article Review Read the article entitled Where Financial Reporting Still Falls Short and write a brief review about the problems stated by the author. Do you agree or disagree? Support your answer by...
-
Robert is an actuarial analyst at one of the Big Five consulting firms in Canada and he has been living with his family to save money. He will turn 35 years of age on the 1st of April 2021 and is...
-
How do population genetics principles such as mutation rate, allele frequency, and genetic drift shape the evolutionary trajectory of species in response to environmental pressures?
-
Special education services are funded through different sources and agencies. Describe services available for children in three different age groups: Birth-3, 3-5, and 5-8. Which age group has the...
-
How do mutational processes such as base substitutions, insertions, deletions, and chromosomal rearrangements occur, and what are their implications for genome stability?
-
Q4: A heat transfer experiment in which convection and radiation phenomenon took place was carried out with the experiment system which specifications given below. The data obtained from the...
-
Statement of Cash Flows, Operating Activities Section, Indirect Method. Repeat the requirements of P6-9 under the indirect method. P6-9 Statement of Cash Flows, Operating Activities Section, Direct...
-
Provide examples of a situations in which environmental disruptions affected consumer attitudes and buying behaviors.
-
Sherry Hopson owns a retail family clothing store. Her store is located at 4321 Circle Drive, Houston, TX 77001. Her employer identification number is 95- 1234321 and her Social Security number is...
-
Frank Willingham has the following transactions during the year: Sale of office equipment on March 15 that cost $20,000 when purchased on July 1, 2010. Frank has claimed $5,000 in depreciation and...
-
Ann hires a nanny to watch her two children while she works at a local hospital. She pays the 19-year-old nanny $125 per week for 48 weeks during the current year. a. What is the employer's portion...
-
Describe the nature and significance of the biology plus approach to establishing parental rights for unwed fathers.
-
Identify and describe the three primary ways of establishing legal parentage.
-
Identify the three parties with the strongest interests in parentage determinations and describe the nature of those interests.
Study smarter with the SolutionInn App