Question

IN PYTHON Write the script test3.py that creates and populates a list of OnlineStudent objects. It inputs a requested s...

IN PYTHON

  1. Write the script test3.py that creates and populates a list of OnlineStudent objects. It inputs a requested student, then prints the fields for that student, and also the username of each student in the student's study group. See the pseudocode in Item 3 below.
  2. Use the new type of for loop that you can use for reading from a file:
    # Instead of writing a loop like this
    fin = open("input-file", "r")
    line = fin.readline( )
    while line != "":
        process line
        line = fin.readline( )
    fin.close( )
    
    # write it like this:
    fin = open("input-file", "r")
    for line in fin:
        process line
    fin.close( )
    
  3. Pseudocode for test3.py:
    Initialize list to hold OnlineStudent objects, call it students.
    Open input file students.txt.
    For all lines in the input file
        Use split to extract the fields list.
        Assign variable names to fields.
        Create OnlineStudent object.
        Append object to list.
    End for.
    Close input file object.
    
    Sort array by username.
    
    Print list of OnlineStudent objects.
    
    Open input file study-groups.txt.
    For all lines in input file
        Use split to extract the fields list.
        For each student s in students list
            If s.username is equal to fields[0]
                For the index i in the range 
                    from 1 to length of fields list - 1
    
                    Append fields[i] to s.study_group list.
                End for.
            End if.
        End for.
    End for.
    
    For s in students' list
        Print s.
        Print usernames of study group members.
    End for. 
    
    Input requested username.
    for each student s in students list
       If requested username is equal to s.username
           Print s.
           For t in s.study_group
               Print t    
           End for.
    
    Close input file.               
    
  4. Challenge Problem: Instead of printing the usernames of the students in the study group, print the first names of the students in the study group.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

programming language: python

As myself defined some attributes as fields in the OnlineStudent Class. You can change as you wish.

Save the files in appropriate location.

source code:

class OnlineStudent:
study_group=[]
def __init__(self,username,first_name,last_name,subject,age):
#u can change as u want
self.username=username
self.first_name=first_name
self.last_name=last_name
self.age=age
self.subject=subject
student=[] #student array object
fin = open("students.txt", "r")
for line in fin:
detail=line.split() # name given just for understanding
student.append(OnlineStudent(detail[0],detail[1],detail[2],detail[3],detail[4]))#data will change depends the number #of fields in input file
fin.close()
def keyname(data): # for sorting with username
return data[0]
student.sort(key = keyname)
#printing the data from student list
for data in student:
print(data)
#Opening study_group file and working on it.
fin = open("study-groups.txt", "r")
for line in fin:
fields=line.split()
for s in student:
if s.username==fields[0]:
k=[]
for i in range(1,len(fields)):
k.append(fields[i])
s.study_group.append(k)
fin.close()
for s in student :
print (s)
if s.study_group :
print(s.first_name)#prints the first name of the study group
usrname=input("Enter the username : ")
for s in student:
if usrname==s.username:
print(s)
for t in s.study_group:
print(t)

Add a comment
Know the answer?
Add Answer to:
IN PYTHON Write the script test3.py that creates and populates a list of OnlineStudent objects. It inputs a requested s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Ini...

    Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Initialize the object properties def __init__(self, id, name, mark): # TODO # Print the object as an string def __str__(self): return ' - {}, {}, {}'.format(self.id, self.name, self.mark) # Check if the mark of the input student is greater than the student object # The output is either True or False def is_greater_than(self, another_student): # TODO # Sort the student_list # The output is the sorted...

  • PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything...

    PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything a course uses in a grading scheme, # like a test or an assignment. It has a score, which is assessed by # an instructor, and a maximum value, set by the instructor, and a weight, # which defines how much the item counts towards a final grade. def __init__(self, weight, scored=None, out_of=None): """ Purpose: Initialize the GradeItem object. Preconditions: :param weight: the weight...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • A) Please implement a Python script to define a student class with the following attributes (instance...

    A) Please implement a Python script to define a student class with the following attributes (instance attributes): cwid: student’s CWID first_name : student’s first name last_name: student’s last name gender: student’s gender gpa: student’s gpa Please make these attributes as private ones so they have to be accessed via getter/setter methods or property. For this purpose, please define a getter/setter method and property for each of the attributes. Another thing you need to do is to define a constructor that...

  • Help needed related python task ! Thanx again How you're doing it • Write a function...

    Help needed related python task ! Thanx again How you're doing it • Write a function write_to_file() that accepts a tuple to be added to the end of a file o Open the file for appending (name your file 'student_info.txt') o Write the tuple on one line (include any newline characters necessary) o Close the file • Write a function get_student_info() that o Accepts an argument for a student name o Prompts the user to input as many test scores...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • Write a program in C++ Lightening Lanes Case Study Problem Statement On Tuesday afternoons, Lightening Lanes...

    Write a program in C++ Lightening Lanes Case Study Problem Statement On Tuesday afternoons, Lightening Lanes Bowling Alley runs a special class to teach children to bowl. Each lane has an instructor who works with a team of four student bowlers and instructs them as they bowl three lines (games). The management of Lightening Lanes has asked you to develop a program that will report each student’s 3-game average score and compare it to the average score they bowled the...

  • Write the code to maintain a list of student records. The main flow of your program...

    Write the code to maintain a list of student records. The main flow of your program should be to read all information from file(s) and place it in arrays, loop and do stuff with the records (e.g., add, update, look up, etc.), before exiting optionally write the new information back to the file(s) before exiting. Please note the files are only accessed at the beginning and the end, do not change the files during the "do stuff loop" (unless you...

  • Goal:   Unscramble permuted words by generating all permutations of Jumble string and searching for a word...

    Goal:   Unscramble permuted words by generating all permutations of Jumble string and searching for a word in Unix dictionary. Unix Dictionary: dict.txt Details: Write a method called get_permutations that inputs a string like "dog". Your method should return an array of all permutations of the Jumble string. . For example: s = "dog" perms = get_permutations(a) print(perms) Output: ['dog', 'dgo', 'odg', 'ogd', 'gdo', 'god'] Rewrite the script for obtaining permutations and the end of the Comments, Hints, and Observersions section...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT