Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用While循环(Python)计算GPA_Python_While Loop - Fatal编程技术网

使用While循环(Python)计算GPA

使用While循环(Python)计算GPA,python,while-loop,Python,While Loop,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,还有一个whi

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计算到小数点后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函数?我的代码运行不正确


提前谢谢

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


您不需要
while
循环,因为您使用的是
for
循环。这通常更为惯用且更具可读性,但如果您的作业需要
while
循环,则必须重写它以使用循环。换句话说,您需要为
x
设置起始值,然后使用
while(x上的某些条件):
,然后在循环内部以某种方式更新
x
。那么我是否要用while替换它?while函数是否需要if else elif?否,while循环不需要if/
else
while
语句具有内置条件。您可以改为只使用
,而True:
,并使用
if(与x上的条件相反):break
或类似的函数,有时您必须这样做,但通常这不是必需的。我这样做了,现在代码不会运行。你能告诉我怎么做吗?非常感谢。但是我在运行你的代码时遇到了问题。当我运行它时,我得到了这个错误…回溯(最近一次调用):文件“”,第8行,在文件“”中,第1行,在名称错误:名称“A”未定义如果您使用的是Python 2.7,请使用
raw\u input
而不是
input
<代码>输入尝试将输入解释为Python代码。这就是为什么你会得到这个信息。你不应该使用它,但在特殊情况下。此外,如果要获得小数,则在计算
GPA=float(totpoints)/totunits
时,必须显式转换为
float
。否则,如果两个变量都是整数,Python 2.7将执行楼层分割。我不知道如何确切地改变它,因为它似乎没有运行。那么,我应该在哪个版本的python中运行您的代码吗?我没有编译器,所以我使用在线编译器。非常感谢。它工作得很好。我还有一个问题。你能帮我吗?还有,我如何投票支持你的答案?我是新来的。链接在这里。。。
while <condition>:
    do_something
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