Answer to Question #279028 in Python for Isabel Cortez

Question #279028

Create a program with comments that can record the name, gender, and grades of a student in a certain subject. The grades consist of 1st, 2nd, 3rd, and 4th quarters.



The program should be able to do the following:


• Add new student

• Set student grades

• Show all students w/ grades

• Show student grade remarks (PASS or FAILED), passing mark is >= 75

• Search student record


The program should use the following features:


• Array

• Class

• Specific Sorting Algorithm (Selection)

• Specific Searching Algorithm (Linear)


1
Expert's answer
2021-12-13T16:58:08-0500
class Student:
   def __init__(self):
      self.name = input('Enter name: ').capitalize()
      self.gender = input('Enter gender: ')
      self.grades = []

   def set_grades(self):
      self.grades = [int(input(f'Enter grade №{i}: ')) for i in range(1, 5)]

   def grade_remarks(self):
      for grade in self.grades:
         if grade >= 75:
            print('PASS', end=' ')
         else:
            print('FAILED', end=' ')
         print('')


class Students:
   def __init__(self):
      self.all = []

   def add_student(self):
      self.all.append(Student())

   def show_with_grades(self):
      for i, stud in enumerate(self.all):
         if stud.grades:
            print(f'№{i} Name: {stud.name}')

   def binary(self, n):
      n = n.capitalize()
      low = 0
      high = len(self.all) - 1
      while low <= high:
         mid = low + high
         guess = self.all[mid].name
         if guess == n:
            return mid
         if guess > n:
            high = mid - 1
         else:
            low = mid + 1
      return None

   def _smallest(self):
      smallest = self.all[0].name
      smallest_id = 0
      for i in range(1, len(self.all)):
         if self.all[i].name < smallest:
            smallest = self.all[i].name
            smallest_id = i
      return smallest_id

   def selection(self):
      temp = []
      for i in range(len(self.all)):
         smallest = self._smallest()
         temp.append(self.all.pop(smallest))
      self.all = temp


s = Students()
cm = ''
while ​cm != 'exit':
  ​cm = input('Enter command or student number: ')
  ​if cm == 'add':
     ​s.add_student()
  ​elif cm == 'show':
     ​s.show_with_grades()
  ​elif cm == 'sort':
     ​s.selection()
  ​elif cm == 'search':
     ​print(f'Student numder is: {s.binary(input("Enter name: ").capitalize())}')
  ​elif cm.isdigit():
     ​if 0 <= int(cm) < len(s.all):
        ​num = int(cm)
        ​print(f'Name: {s.all[num].name}')
        ​cm = input('Enter command: ')
        ​if cm == 'set':
           ​s.all[num].set_grades()
        ​elif cm == 'remarks':
           ​s.all[num].grade_remarks()
     ​else:
        ​print('Out of range')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS