在 Python 中使用函数计算 GPA

Posted

技术标签:

【中文标题】在 Python 中使用函数计算 GPA【英文标题】:Calculating GPA with function in Python 【发布时间】:2020-12-30 12:29:14 【问题描述】:

好的,所以我正在尝试制作一个程序,用户必须在其中输入课程的字母等级以及课程学分。总共有三门课程。它将每门课程的所有学分相加(学分*课程学分),然后除以三门课程的总学分。

所以我在输入变量中输入了这个,结果一切正常:

Enter your course 1 letter grade: A
Enter your course 1 credit: 3
Grade point for course 1 is: 4.0
Enter your course 2 letter grade: B
Enter your course 2 credit: 2
Grade point for course 2 is: 3.0
Enter your course 3 letter grade: C
Enter your course 3 credit: 4
Grade point for course 3 is: 2.0

总 GPA 应该是 2.8888888888889,但我的程序打印出 18.88888888888889。有人可以告诉我我在这里做错了什么吗?

这是我的代码:

def getGradePoint(courseLetterGrade):
  if (courseLetterGrade == "A"):
    return 4.0;
  elif (courseLetterGrade == "A-"):
    return 3.67;
  elif (courseLetterGrade == "B+"):
    return 3.33;
  elif (courseLetterGrade == "B"):
    return 3.0;
  elif (courseLetterGrade == "B-"):
    return 2.67;
  elif (courseLetterGrade == "C+"):
    return 2.33;
  elif (courseLetterGrade == "C"):
    return 2.0;
  elif (courseLetterGrade == "D"):
    return 1.0;
  else:
    return 0.0;
  
# Main area of code - Code that allows the user to input the variables and then calls to the getGradePoint function
def run():  
  courseLetterGrade = str(input("Enter your course 1 letter grade: "))
  courseCredit = float(input("Enter your course 1 credit: "))
  print(f"Grade point for course 1 is getGradePoint(courseLetterGrade).")
  
  courseLetterGrade2 = str(input("Enter your course 2 letter grade: "))
  courseCredit2 = float(input("Enter your course 2 credit: "))
  print(f"Grade point for course 2 is getGradePoint(courseLetterGrade2).") 
  
  courseLetterGrade3 = str(input("Enter your course 3 letter grade: "))
  courseCredit3 = float(input("Enter your course 3 credit: "))
  print(f"Grade point for course 3 is getGradePoint(courseLetterGrade3).")

  GPA = (getGradePoint(courseLetterGrade) * courseCredit) + (getGradePoint(courseLetterGrade2) * courseCredit2) + (getGradePoint(courseLetterGrade3) * courseCredit3) / (courseCredit + courseCredit2 + courseCredit3)

  print(f"Your GPA is: str(GPA)")

if __name__ == "__main__":
  run()

【问题讨论】:

【参考方案1】:

注意你的操作顺序。

你得到的是:

(4*3) + (3*2) + ((2*4)/(3+2+4)) = 18.88...

但你想要的是

((4*3) + (3*2) + (2*4)) / (3+2+4) = 2.88...

除法运算符/+ 具有更高的优先级,因此没有任何括号表示首先进行所有加法除法之前,它将首先使用最后一个加权分数进行除法,然后添加其他加权成绩。

有关运算符优先级的更多信息:6.17. Operator precedence

How do order of operations go on Python?

【讨论】:

【参考方案2】:

你应该用它来计算

GPA = ((getGradePoint(courseLetterGrade) * courseCredit) + (getGradePoint(courseLetterGrade2) * courseCredit2) + (getGradePoint(courseLetterGrade3) * courseCredit3)) / (courseCredit + courseCredit2 + courseCredit3)

遵循操作顺序 PEMDAS 的规则。因为/ 是首先使用的,没有括号。 . 以供参考。 https://www.mathsisfun.com/operation-order-pemdas.html

【讨论】:

以上是关于在 Python 中使用函数计算 GPA的主要内容,如果未能解决你的问题,请参考以下文章

python 从csv文件计算GPA

python UTMATE GPA计算用スクリプト

将字符串除以整数以获得 GPA 计算

需要创建 GPA 计算器才能在命令行中运行

如何在这个 GPA 计算器中构建我的 for 循环?爪哇

使用一组学生及其各自的 GPA 对 GPA 进行各种计算