【问题标题】:Calculting GPA using While Loop (Python)使用 While 循环 (Python) 计算 GPA
【发布时间】:2014-10-02 04:50:02
【问题描述】:

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

您的程序将有一个用于计算多个 GPA 的 while 循环和一个用于收集个人成绩的 while 循环(即嵌套的 while 循环)。

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

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

提前致谢。

【问题讨论】:

  • 您不需要while 循环,因为您使用的是for 循环。这通常更惯用且更具可读性,但如果您的作业需要 while 循环,则必须重写它以使用它。
  • 换句话说:您需要为x设置一个起始值,然后使用while (some condition on x):,然后在循环内部以某种方式更新x
  • 所以我只是用while替换它吗? while 函数不需要 if else elif 吗?
  • 不,while 循环不需要if/elsewhile 语句内置了一个条件。您可以改为使用 while True:,并在内部使用 if (opposite of condition on x): break 或类似的,有时您必须这样做,但通常这不是必需的.
  • 我做了,现在代码无法运行。你能告诉我怎么做吗?

标签: python while-loop


【解决方案1】:

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

while <condition>:
    do_something

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

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

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   

【讨论】:

  • 非常感谢。但我无法运行您的代码。当我运行它时,我收到此错误.... Traceback (last recent call last): File "", line 8, in File "", line 1, in NameError : 名称“A”未定义
  • 如果您使用的是 Python 2.7,请使用 raw_input 而不是 inputinput 尝试将输入解释为 Python 代码。这就是您收到该消息的原因。你不应该使用它,但在特殊情况下。此外,如果您想获得小数,则必须在计算 GPA = float(totpoints)/totunits 时显式转换为 float。否则,如果两个变量都是整数,Python 2.7 将执行除法。
  • 射击。我不知道如何完全改变它,因为它似乎没有运行。那么我应该在什么版本的python中运行你的代码?我没有编译器,所以我使用在线编译器。
  • 非常感谢。它工作得很好。我有另一个问题。你能帮帮我吗?另外,我如何投票给你答案?我是该网站的新手。链接在这里...stackoverflow.com/questions/26151660/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 2012-03-23
  • 2021-12-19
  • 2020-12-30
  • 2017-08-09
  • 2017-07-23
相关资源
最近更新 更多