GPA 计算器(除以零)- Python

GPA Calculator (division by zero) - Python

这是我的简单 GPA 计算器代码。我收到一个错误(见报告),即使列表中有一门课程,因为我按照输入输入课程。可能跟调用displayOutputTable函数的位置有关?

class Course(object):
    def __init__(self, courseName, letterGrade):
        self.courseName = courseName
        self.letterGrade = letterGrade


courseList = []

def acceptInput():
   courseList = []
   choice = input("Type ‘a’ to add new class or ‘e’ to end. ")
   if choice == 'a':
       courseName = input("Class Name? ")
       letterGrade = input("Grade? ")
   course = Course(courseName, letterGrade)
   courseList = courseList + [course]   
   print(courseList)
    # Create Course object using local data from user 
    # Add this new course object to courseList list 

def convertGradeToPoints(letterGrade):
  creditValue = 0  
  for i in courseList:
        if letterGrade == "A":
           creditValue = 4.0
        if letterGrade == "B":
           creditValue = 3.0
        if letterGrade == "C":
           creditValue = 2.0
        if letterGrade == "D":
           creditValue = 1.0
        if letterGrade == "F":
           creditValue = 0.0


        return creditValue

def calculateGPA():
    numbercourseList = len(courseList)
    totalpoints = 0
    for course in courseList:
        totalpoints  =  totalpoints + convertGradeToPoints(course.letterGrade)
    return totalpoints/numbercourseList

def displayOutputTable():
    print ("COURSE NAME \t LETTER GRADE \t POINTS")
    print("-------------------------")
    for course in courseList:
        print(course.courseName + "\t" + course.letterGrade + "\t" + convertGradeToPoints(course.letterGrade))   


    print("Total unweighted gpa" + "\t" + str(calculateGPA()))

acceptInput()
displayOutputTable()

这是错误报告:

Traceback (most recent call last):
  File "C:/Users/Fred/Desktop/tester.py", line 56, in <module>
    displayOutputTable()
  File "C:/Users/Fred/Desktop/tester.py", line 53, in displayOutputTable
    print("Total unweighted gpa" + "\t" + str(calculateGPA()))
  File "C:/Users/Fred/Desktop/tester.py", line 44, in calculateGPA
    return totalpoints/numbercourseList
ZeroDivisionError: int division or modulo by zero

课程[]是一个全局变量。您只能通过首先将全局变量声明为全局以将全局名称绑定到局部范围来在局部范围内重新分配全局变量。否则,当您执行 courseList = [] 时,实际发生的是一个名为 courseList 的新变量在本地范围内分配了一个列表。

def acceptInput():
   global courseList
   courseList = []
   choice = input("Type ‘a’ to add new class or ‘e’ to end. ")
   if choice == 'a':
       courseName = input("Class Name? ")
       letterGrade = input("Grade? ")
   course = Course(courseName, letterGrade)
   courseList = courseList + [course]   
   print(courseList)
    # Create Course object using local data from user 
    # Add this new course object to courseList list 

或者,只改变 LIST 对象而不重新分配它的方法。

def acceptInput():
       courseList.clear()
       choice = input("Type ‘a’ to add new class or ‘e’ to end. ")
       if choice == 'a':
           courseName = input("Class Name? ")
           letterGrade = input("Grade? ")
       course = Course(courseName, letterGrade)
       courseList.append(course)   
       print(courseList)
        # Create Course object using local data from user 
        # Add this new course object to courseList list 

另外,不确定为什么每次接受新课程输入时都想清除课程列表。这样您将只能上一门课程。

针对您的评论进行了编辑:

How do I make it so the console keeps asking me for input after I submit one class

答案是使用循环结构,这是目前所有命令式编程语言的基本结构。或许你最好考虑学习基本的 python 教程,例如 LearnPythonTheHardWay,如果你不知道的话。

我还将包含一个 sigterm/sigint 处理程序来毫无例外地处理 ctrl+c。

import signal
stop = False
def exit():
    global stop
    stop = True
signal.signal(signal.SIGINT, lambda signum, frame: exit())
signal.signal(signal.SIGTERM, lambda signum, frame: exit())
while not stop:
    acceptInput()
    displayOutputTable()