使用 While 循环 (Python) 计算 GPA [英] Calculting GPA using While Loop (Python)

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

问题描述

GPA 或平均学分绩点的计算方法是将学生在课程中获得的学分绩点相加,然后除以总学分.单门课程的成绩点是通过将该课程的学分乘以适当的系数来计算的,具体取决于所获得的成绩:

 A 获得 4 个学分B 获得 3 个学分C 获得 2 个学分D 获得 1 个学分F 获得 0 分

您的程序将有一个 while 循环来计算多个 GPA,并有一个 while 循环来收集单个成绩(即嵌套的 while 循环).

对于您的演示,计算这两个课程组合的 GPA 到小数点后两位:

 第一种情况:5 个单位 A 4 个单位 B 3 个单位 C第二种情况: 6 个单位 A 6 个单位 B 4 个单位 C

这就是我目前所拥有的......

todo = int(input("你想计算多少 GPA?"))while True: x in range (1, todo+1)n = int(input("你要输入几门课?"))总分 = 0总单位 = 0而真:范围(1,n + 1)Grade = input("请输入课程成绩:")如果成绩=='A':等级 = 整数(4)如果成绩=='B':等级 = 整数(3)如果成绩=='C':等级 = 整数(2)如果成绩=='D':等级 = 整数(1)如果成绩=='F':等级 = 整数(0)units = int(input("这门课有多少个单元?"))总单位 += 单位分数=等级*单位总分 += 分GPA = 总分/总单位打印(GPA是",(%.2f"%GPA))打印(总分=,总分)打印(总单位=,totunits)

我的问题是如何正确地合并 while 函数?我的代码运行不正常.

提前致谢.

解决方案

关于 Python,您必须了解的第一件事就是缩进.因此,请确保正确缩进 while 块.while 语句的语法如下:

<块引用>

while <条件>:做点什么

如果条件为真,则执行 while 块中的代码.执行代码后,再次检查条件.如果条件仍然为真,它将再次执行该块,依此类推,直到条件为假.一旦条件为假,它就会退出 while 块并继续执行之后的代码.

最后一件事,Python 不像其他编程语言那样有 case 语句.但是您可以使用字典,例如我定义了一个字典来将成绩映射到分数并从代码中删除 if 语句:

<块引用>

gradeFactor = {'A':4, 'B': 3, 'C':2, 'D':1, 'F':0}todo = int(raw_input("你想计算多少 GPA?"))待办事项:n = int(raw_input("你要输入多少门课?"))总分 = 0总单位 = 0而 n:Grade = raw_input("请输入课程成绩:")units = int(raw_input("这门课有多少个单元?"))总单位 += 单位分数 = 等级因子[等级]*单位总分 += 分n -= 1GPA = float(totpoints)/totunits打印(GPA是",(%.2f"%GPA))打印(总分=,总分)打印(总单位=,totunits)待办事项 -= 1

A GPA, or Grade point Average, is calculated by summing the grade points earned in a student’s courses and then dividing by the total units. The grade points for an individual course are calculated by multiplying the units for that course by the appropriate factor depending upon the grade received:

  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

Your program will have a while loop to calculate multiple GPAs and a while loop to collect individual grades (i.e. a nested while loop).

For your demo, calculate the GPA to 2 decimal places for these two course combinations:

 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

This is what I have so far....

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)    

My question is how do I exactly incorporate the while function correctly? My code is not running correctly.

Thanks in advance.

解决方案

First thing you must know about Python is that is all about indentation. So make sure you indent your while blocks correctly. The syntax of the while statements is like:

while <condition>:
    do_something

The code inside the while block is executed if the condition is true. After executing the code the condition is checked again. If the condition is still true it will execute the block again, and so on and so forth until the condition is false. Once the condition is false it exits the while block and keeps executing the code after.

One last thing, Python does not have case statements like other programming languages do. But you can use a dictionary, so for example I've defined a dictionary to map the grades to points and remove the if statements from your code:

gradeFactor = {'A':4, 'B': 3, 'C':2, 'D':1, 'F':0}
todo = int(raw_input("How many GPA's would you like to calculate? "))
while todo: 
    n = int(raw_input("How many courses will you input? "))
    totpoints = 0
    totunits = 0
    while n:  
        grade = raw_input("Enter grade for course: " )
        units = int(raw_input("How many units was the course? "))
        totunits += units
        points = gradeFactor[grade]*units
        totpoints += points
        n -= 1
    GPA = float(totpoints) / totunits
    print("GPA is ", ("%.2f" % GPA))
    print("total points = ", totpoints)
    print("total units = ", totunits)
    todo -= 1   

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

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