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