calculating the average school grade which is an integer. The current code doesn't calculate the average grade correctly. Also when I enter 0 to exit it get an error (please run code on your end to find the issues with calculating the average grade and error when enter 0).

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question
100%

need help finishing Python code. I need help calculating the average school grade which is an integer. The current code doesn't calculate the average grade correctly. Also when I enter 0 to exit it get an error (please run code on your end to find the issues with calculating the average grade and error when enter 0). 

CODE 

import decimal
from decimal import Decimal

class GradeBook:   
    count = 0          
    def __init__(self, name, grades=[]):      
        self.name = name          
        GradeBook.count += 1          
        print("There are",GradeBook.count,"students in the GradeBook")     
        self.grades = []       
    def quizScore(self,score):       
        self.grades.append(score)           
    def currentAverage(self):        
        sum = 0     
        for i in self.grades:    
            sum += i   
            avg = sum/len(self.grades)     
            return avg     
name1 = input("Please enter the name for Student 1:") 
Student1 = GradeBook(name1) 
name2 = input("Please enter the name for Student 2:") 
Student2 = GradeBook(name2) 

menu = """
Grade Book

0: Exit
1: Enter quiz grade for Student 1
2: Enter quiz grade for Student 2
3: Display current grades for all students
"""
done = False 

while not done:   
    print(menu)
    selection = input('Please enter a choice: ')
 
    if selection == "0":
        done = True
        print('Exiting Grade Book Now!')  

    if selection == '1':     
        print('Student 1')     
        grade = float(input("Enter the grade for the test: "))   
        Student1.quizScore(grade)      
            
    elif selection == '2':     
        print('Student 2')     
        grade = float(input("Enter the grade for the test: "))     
        Student2.quizScore(grade)       
        
    elif selection == '3':     
        print("Current grades for Students")     
        print("Name:",Student1.name)             
        print("Current Average:",Student1.currentAverage())         
        print("Name:",Student2.name)        
        print("Current Average:",Student2.currentAverage())   
Score(" ", [])
obj.quizScore([])
obj.display()    

        

OUTPUT  OF CODE

Please enter the name for Student 1:
bob
There are 1 students in the GradeBook
Please enter the name for Student 2:
tom
There are 2 students in the GradeBook

Grade Book

0: Exit
1: Enter quiz grade for Student 1
2: Enter quiz grade for Student 2
3: Display current grades for all students

Please enter a choice: 
1
Student 1
Enter the grade for the test: 
33

Grade Book

0: Exit
1: Enter quiz grade for Student 1
2: Enter quiz grade for Student 2
3: Display current grades for all students

Please enter a choice: 
1
Student 1
Enter the grade for the test: 
22

Grade Book

0: Exit
1: Enter quiz grade for Student 1
2: Enter quiz grade for Student 2
3: Display current grades for all students

Please enter a choice: 
3
Current grades for Students
Name: bob
Current Average: 16.5
Name: tom
Current Average: None

Grade Book

0: Exit
1: Enter quiz grade for Student 1
2: Enter quiz grade for Student 2
3: Display current grades for all students

Please enter a choice: ** Process Stopped **

Press Enter to exit terminal

Expert Solution
Step 1

Built in Sum () is used to sum up the individual list items. 

To avoid divide by zero error in case of blank grades list for any of the students, length of the list becomes 1 and as sum gives 0, it returns None as in the sample output.

Selection Choice =0 , works fine. No change required.

Importing Decimal not needed as no use of it.

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education