我正在使用GPA计算器来熟悉python /编程。当每个班级都有输入时,我可以使用它。但是,我不确定如果没有为学分/年级输入任何内容或将其用作空白,该如何跳过该变量。
我尝试将空白字符串添加到字典并将其设置为None,但是仍然出现错误。
grades = {
'A+' : 4.00,
'A' : 4.00,
'A-' : 3.67,
'B+' : 3.33,
'B' : 3.00,
'B-' : 2.67,
'C+' : 2.33,
'C' : 2.0,
'C-' : 1.67,
'D+' : 1.33,
'D' : 1.0,
'F' : 0.0,
}
grd_num = []
cred = []
grd_num.append(grades[input('Enter the letter grade for your first class\n')])
cred.append(float(input('Enter the amount of credits that your first class is worth\n')))
grd_num.append(grades[input('Enter the letter grade for your second class\n')])
cred.append(float(input('Enter the amount of credits that your second class is worth\n')))
grd_num.append(grades[input('Enter the letter grade for your third class\n')])
cred.append(float(input('Enter the amount of credits that your third class is worth\n')))
grd_num.append(grades[input('Enter the letter grade for your fourth class\n')])
cred.append(float(input('Enter the amount of credits that your fourth class is worth\n')))
grd_num.append(grades[input('Enter the letter grade for your fith class\n')])
cred.append(float(input('Enter the amount of credits that your fith class is worth\n')))
grd_num.append(grades[input('Enter the letter grade for your sixth class\n')])
cred.append(float(input('Enter the amount of credits that your sixth class is worth\n')))
grd_num.append(grades[input('Enter the letter grade for your seventh class\n')])
cred.append(float(input('Enter the amount of credits that your seventh class is worth\n')))
grd_num.append(grades[input('Enter the letter grade for your eighth class\n')])
cred.append(float(input('Enter the amount of credits that your eighth class is worth\n')))
totGPA = ((grd_num[0] * cred[0]) + (grd_num[1] * cred[1]) + (grd_num[2] * cred[2]) + (grd_num[3] * cred[3]) + (grd_num[4] * cred[4]) + (grd_num[5] * cred[5]) + (grd_num[6] * cred[6]) + (grd_num[7] * cred[7]))/sum(cred)
print(totGPA)
我希望用户能够输入从1到8个任意数量的班级。现在,用户只能输入8个班级而不会出错。
答案 0 :(得分:2)
这是我可以想到的解决方法。您可以检查用户输入是否为空,并相应地中断输入序列。
$row2
通过检查用户输入是否为空,可以有效地停止提示输入,并用零填充剩余数据。