python如何计算个人gpa_使用While循环(Python)计算GPA
GPA或平均分數是通過將學生課程中獲得的分數相加,然后除以總單位計算出來的。對于個別課程的等級分是由根據等級的相應系數為課程單位乘以收到:使用While循環(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到小數點后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函數?我的代碼運行不正常。
在此先感謝。
+1
您不需要'while'循環,因為您'重新使用'for'循環代替。這通常更具慣用性和可讀性,但如果你的任務需要一個'while'循環,你將不得不重寫它以使用它。 –
2014-09-30 19:40:22
+0
換句話說:您需要為'x'設置一個起始值,然后使用'while(某些條件在x上):',然后在循環內部以某種方式更新'x'。 –
2014-09-30 19:41:01
+0
所以我只是用它來替換它?如果其他的elif函數不需要嗎? –
2014-09-30 19:41:37
總結
以上是生活随笔為你收集整理的python如何计算个人gpa_使用While循环(Python)计算GPA的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 表单PostGet两个长度限制问题的分析
- 下一篇: 基于python的系统构建_搭建一个基于