Calculting GPA using While Loop (Python)

馋奶兔 提交于 2019-12-13 08:14:50

问题


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.


回答1:


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   


来源:https://stackoverflow.com/questions/26128722/calculting-gpa-using-while-loop-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!