2017-08-06 118 views
-3

我的GPA計算器似乎無法正常工作。我輸入的任何值的組合將返回0.0的GPA。代碼在我的IDE中格式正確,但我不知道如何縮進這裏,對不起。GPA計算器始終返回0.0

def gpacalculator(grade,credit): 
    gradescore = 0 
    gradescore = int(gradescore) 
    if grade in ("A","Z"): 
     gradescore = 4*credit 
    elif grade == "B+": 
     gradescore = 3.5*credit 
    elif grade == "B": 
     gradescore = 3*credit 
    elif grade == "C+": 
     gradescore = 2.5*credit 
    elif grade == "C": 
     gradescore = 2*credit 
    elif grade == "D+": 
     gradescore = 1.5*credit 
    elif grade == "D": 
     gradescore = 1*credit 
    elif grade == "F": 
     gradescore = 0*credit 

    return gradescore 

subject = input("How many subjects would you like to calculate?") 
subject = int(subject) 
print("The different levels of grades are: Z, A, B+, B, C+, C, D+, D, F") 
a = 0 
a = int(a) 
for a in range(0,subject): 
    grade = str(input("What's your grade for subject number " + str(a+1) + "?")) 
    grade.upper() 
    credit = int(input("What's the number of credits for subject number " + str(a+1) + "?")) 

totalcredit = 0 
totalcredit - int(totalcredit) 
totalcredit+=credit 

totalgradescore = 0 
totalgradescore = int(totalgradescore) 
totalgradescore += gpacalculator(grade,credit) 

gpa = totalgradescore/totalcredit 
print("Your GPA is: ",gpa) 
+0

有一個錯字'totalcredit - INT(totalcredit)',應該是'totalcredit = INT(totalcredit)'。之後,這是正常工作。 – Ofisora

+0

修復代碼工作的縮進。然後想一想,當你總是將變量置零,然後添加一些東西時會發生什麼。 –

回答

1

第一個錯誤是totalcredit - int(totalcredit)我得到完全擺脫它,因爲沒有必要使totalcreditint,如果你把它聲明等於0同爲變量totalgradescorea。您遇到的下一個問題是grade.upper,您需要設置grade = grade.upper()。最後,您需要縮進credittotalcredit的加法,並將您的返回值縮小爲totalgradescore,因爲您希望它們在循環中,因爲您要爲每個課程更改它們,並將它們的聲明移至for循環之前,因爲您不需要希望他們在每次迭代時重置。

最後的代碼應該是這個樣子:

def gpacalculator(grade,credit): 
    gradescore = 0 
    gradescore = int(gradescore) 
    if grade in ("A","Z"): 
     gradescore = 4*credit 
    elif grade == "B+": 
     gradescore = 3.5*credit 
    elif grade == "B": 
     gradescore = 3*credit 
    elif grade == "C+": 
     gradescore = 2.5*credit 
    elif grade == "C": 
     gradescore = 2*credit 
    elif grade == "D+": 
     gradescore = 1.5*credit 
    elif grade == "D": 
     gradescore = 1*credit 
    elif grade == "F": 
     gradescore = 0*credit 
    return gradescore 

subject = input("How many subjects would you like to calculate?") 
subject = int(subject) 
print("The different levels of grades are: Z, A, B+, B, C+, C, D+, D, F") 
totalgradescore = 0 
totalcredit = 0 

for a in range(subject): 
    grade = str(input("What's your grade for subject number " + str(a+1) + "?")) 
    grade = grade.upper() 
    credit = int(input("What's the number of credits for subject number " + str(a+1) + "?")) 
    totalcredit += credit 
    totalgradescore += gpacalculator(grade,credit) 

gpa = totalgradescore/totalcredit 
print("Your GPA is: ", gpa) 
0

你的問題很簡單

string.upper(s)返回s的副本,但帶有轉換爲大寫小寫字母。

這意味着您必須將返回值賦給變量。如果您測試輸入大寫字母等級的程序,它可以正常工作。

因此,只要這一變化

for a in range(0,subject): 
    grade = str(input("What's your grade for subject number " + str(a+1) + "?")) 
    grade = grade.upper() 
    credit = int(input("What's the number of credits for subject number " + str(a+1) + "?")) 

而只是作爲一個額外的小費,如果你指定一個文字數字,沒有小數點點變量,該變量的類型將是整數,則不需要在下一行轉換它。你有

+0

謝謝!我的代碼現在可用。我想我過了一段時間沒有進行簡單的編程就生鏽了...... – NewProgrammer

+0

Ofisora也是對的,那行代碼有一個錯字,它沒有對你的程序做任何無害的事情,因爲它是一個不必要的行。 –