用python计算绩点的代码_使用While循环计算GPA(Python)

平均绩点(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函数?我的代码运行不正确。

提前谢谢。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值