GPA计算器(除以零) - Python [英] GPA Calculator (division by zero) - Python

查看:138
本文介绍了GPA计算器(除以零) - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一个简单的GPA计算器的代码。即使列表中有一个课程,但我收到错误(请参阅报告),因为我按照输入并输入了课程。也许它与调用displayOutputTable函数的位置有关?

This is my code for a simple GPA calculator. I am getting an error (see report) even though there is one course in the list as I follow the input and enter in the course. Maybe it has something to do with the placement of the call to the displayOutputTable function?

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()

这里是错误报告:

here is the error report:

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


推荐答案

course []是一个全局变量。只能通过声明全局变量来将全局名称绑定到本地作用域,才能在本地作用域中重新分配全局变量。否则,当您执行 courseList = [] 时,实际发生的情况是名为courseList的新变量在本地作用域中被分配一个列表。

course[] is a global variable. You can only reassign a global variable in a local scope by first declaring it global to bind the global name to the local scope. Otherwise, when you do courseList = [], what's actually happening is a new variable named courseList is assigned a list in the local scope.

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对象进行变异而不重新分配它。

Alternatively, the way to do it by only mutating the LIST object without REASSIGNING it.

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 

另外,不确定为什么你每次接受新课程的输入时都想清除你的课程列表。你只能以这种方式选择一门课程。

Also, not sure why you want to clear your list of courses every time you accept input of a new course. You'll only ever have one course this way.

已编辑为了您的评论:


我如何做到这一点,所以控制台在我
提交一个类之后一直要求我输入

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

对此的回答是使用一个循环构造,它是每个命令式编程语言的基本构造。如果你不知道这一点,可以考虑从基本的Python教程(如LearnPythonTheHardWay)学习。

The answer to this is to use a looping construct, which is a fundamental basic construct of every imperative programming language out there. Perhaps you'd do well to consider learning from a basic python tutorial such as LearnPythonTheHardWay if you do not know this.

我还将包含一个

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()

这篇关于GPA计算器(除以零) - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆