我的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)
有一個錯字'totalcredit - INT(totalcredit)',應該是'totalcredit = INT(totalcredit)'。之後,這是正常工作。 – Ofisora
修復代碼工作的縮進。然後想一想,當你總是將變量置零,然後添加一些東西時會發生什麼。 –