我的GPA计算器似乎不起作用。我输入的任何值组合都将返回0.0的GPA。代码在我的IDE中格式正确,但我不知道如何在这里缩进,抱歉。
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 :(得分:1)
你遇到的第一个错误是totalcredit - int(totalcredit)
我完全摆脱了它,因为如果你声明它等于0,则不需要将totalcredit
变成int
。变量{同样{1}}和totalgradescore
。您遇到的下一个问题是a
,您需要设置grade.upper
。最后,您需要将grade = grade.upper()
添加到credit
,并将您的返回值缩进到totalcredit
,因为您要在每个课程中更改它们,并移动它们在for循环之前声明,因为你不希望它们在每次迭代时重置。
最终代码应如下所示:
totalgradescore
答案 1 :(得分: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) + "?"))
另外,如果您将一个没有小数点的文字编号分配给变量,那么变量的类型将是整数,您不需要在下一行中转换它。