【问题标题】:Python GPA CalculatorPython GPA 计算器
【发布时间】:2015-02-03 22:22:02
【问题描述】:

我正在使用 python 2 开发 GPA 计算器。但是,我无法让代码按我的意愿工作。我希望有人能帮我出来,给我一些方向。当我输入成绩时,我希望它计算 GPA。现在,它只读取一个字母或一个符号,但不能同时读取。我会输入 A+,它会给我 0.3 而不是 4.3。如果我输入多个等级,它只会读取一年级。 for 部分取所有输入的成绩,并给出我们的平均 GPA。

代码如下:

from sys import argv

def gp(grade):
    points = 0
    if grade == 'A' or grade == 'a':
        points +=  4.0
    if grade == 'B' or grade == 'b':
        points += 3.0
    if grade == 'C' or grade == 'c':
        points += 2.0
    if grade =='D' or grade == 'd':
        points += 1.0
    if grade == 'F' or  grade =='f':
        points += 0.0
    if grade.endswith ('+'):
        points = points + 0.3
    if grade.endswith ('-'):
        points = points - 0.3
    for x in grade
        return points = sum(points)/len(grade)

if __name__ == '__main__':
    grade = argv[1].replace(" ","")
    print (("%.1f") % gp(grade))

【问题讨论】:

  • 您希望它如何工作以及现在如何工作?
  • 你有什么问题?
  • 你试过grade[0] == 'A'吗?从您的代码来看,grade 似乎可以是 A+ 和 'A+' != 'A'
  • 另外,for x in grade return points = sum(points)/len(grade) 应该做什么?
  • 成绩不能以两个不同的字符结尾,也不能同时是两个不同的字符

标签: python calculator


【解决方案1】:

由于这看起来像家庭作业,我不会给出解决方案。但我会提供更多关于什么是错误的信息。希望如果您了解问题所在,您将能够弄清楚如何正确处理。

如果你的成绩是'A+',那么它是一个由2个字符组成的字符串,第一个是A,第二个是+。 grade=='A' 可以是真的吗?一旦您理解了这一点,您就应该清楚为什么 A+ 没有获得第 4 部分的成绩。

至于为什么只给你一年级的结果,argv[1]变成了什么?是你发给它的所有成绩吗?

【讨论】:

    【解决方案2】:
    from sys import argv
    
    def gp(grade):
        points = 0
        if grade.lower() == 'a':
            points +=  4.0
        if grade.lower() == 'b':
            points += 3.0
        if grade.lower() == 'c':
            points += 2.0
        if grade.lower() == 'd':
            points += 1.0
        if grade.lower() =='f':
            points += 0.0
        if grade[:0] == "+"
            points = points + 0.3
        if grade[:0] == "-"
            points = points - 0.3
        for x in grade
            return points = sum(points)/len(grade)
    
    if __name__ == '__main__':
        grade = argv[1].replace(" ","")
        print (("%.1f") % gp(grade))
    

    我“修复”了成绩字母的 if 语句,但你在这里是为了符号所以在那里。

    【讨论】:

    【解决方案3】:
    '''This is a simple GPA calculator I was able to put together. Hope this help'''
    
    class GpaCalculator():
        '''Declare the variables'''
        count = 0
        hrs   = 0
        numberofclasses =0
        totalhours = 0
        totalPoints = 0.0
        gpa = 0.0
        '''Prompt the user for the number of classes taking'''
        numberofclasses = int(input("Enter number of  classes "))
    
        '''use for to loop '''
        for count in range(count, numberofclasses):
            '''This is to keep track of the number of classes (Optional)'''
            print("For class # ", count+1)
    
            '''Prompt user for number of number of credit hours per class'''
            hrs = int(input("Enter the credit hrs "))
    
            '''Prompt user to enter the letter grade'''
            grade = input("Enter the letter grade ")
    
            '''Use if statement to check the grade and increment points and total hours'''
    
            if grade == 'A' or grade == 'a':
                totalPoints = totalPoints + (hrs * 4)
                totalhours = totalhours + hrs
            elif grade == 'B' or grade == 'b':
                totalPoints += (hrs * 3.0)
                totalhours += hrs
            elif grade == 'C' or grade == 'c':
                totalPoints += (hrs * 2.0)
                totalhours += hrs
            elif grade == 'D' or grade == 'd':
                totalPoints += (hrs * 1.0)
                totalhours += hrs
                '''If not A,B, C, D then it must be F. You can write validation to check in other lettes'''
            else:
                totalPoints += (hrs * 0.0)
                totalhours += hrs
        '''Calculate GPA based on the total points and total hours'''
        gpa = totalPoints / totalhours
        print("Your GPA is :", gpa)
    
    def main():
        gpa = GpaCalculator()
    
    if __name__ == '__main__':main()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      相关资源
      最近更新 更多