平均绩点(GPA)是通过学生在课程中获得的分数相加,然后除以总单元来计算的。每门课程的分数是通过将该课程的单元乘以适当的系数来计算的,具体取决于所收到的分数:A receives 4 grade points
B receives 3 grade points
C receives 2 grade points
D receives 1 grade point
F receives 0 grade point
你的程序将有一个while循环来计算多个gpa,还有一个while循环来收集个人成绩(即嵌套的while循环)。
对于您的演示,计算这两个课程组合的GPA到小数点后两位:First Case: 5 units of A 4 units of B 3 units of C
Second Case: 6 units of A 6 units of B 4 units of C
这就是我目前所拥有的。。。。todo = int(input("How many GPA's would you like to calculate? "))
while True: x in range (1, todo+1)
n = int(input("How many courses will you input? "))
totpoints = 0
totunits = 0
while True: range(1, n+1)
grade = input("Enter grade for course: " )
if grade == 'A':
grade = int(4)
if grade == 'B':
grade = int(3)
if grade == 'C':
grade = int(2)
if grade == 'D':
grade = int(1)
if grade == 'F':
grade = int(0)
units = int(input("How many units was the course? "))
totunits += units
points = grade*units
totpoints += points
GPA = totpoints / totunits
print("GPA is ", ("%.2f" % GPA))
print("total points = ", totpoints)
print("total units = ", totunits)
我的问题是如何正确地合并while函数?我的代码运行不正确。
提前谢谢。