python-3.x - 处理数学时如何“跳过”空白输入?(Python)
问题描述
我正在研究 GPA 计算器以熟悉 python/编程。当每个班级都有输入时,我让它工作。但是,我不确定如果没有为课程学分/等级输入任何内容,或者使其充当空白数字,我将如何跳过该变量。
我尝试向字典中添加一个空白字符串并将其设置为无,但仍然出现错误。
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 个课程而不会出错。
解决方案
这是我能想到的解决方法。您可以检查用户输入是否为空并相应地中断输入序列。
grd = input('Enter the letter grade for your first class\n')
if grd != "":
grd_num.append(grades[grd])
cred.append(float(input('Enter the amount of credits that your first class is worth\n')))
else:
grd_num.append(0.0)
cred.append(0.0)
if grd != "":
grd = input('Enter the letter grade for your second class\n')
if grd != "":
grd_num.append(grades[grd])
cred.append(float(input('Enter the amount of credits that your second class is worth\n')))
else:
grd_num.append(0.0)
cred.append(0.0)
else:
grd_num.append(0.0)
cred.append(0.0)
if grd != "":
grd = input('Enter the letter grade for your third class\n')
if grd != "":
grd_num.append(grades[grd])
cred.append(float(input('Enter the amount of credits that your third class is worth\n')))
else:
grd_num.append(0.0)
cred.append(0.0)
else:
grd_num.append(0.0)
cred.append(0.0)
# REPEAT REPEAT REPEAT ...
if grd != "":
grd = input('Enter the letter grade for your eighth class\n')
if grd != "":
grd_num.append(grades[grd])
cred.append(float(input('Enter the amount of credits that your eighth class is worth\n')))
else:
grd_num.append(0.0)
cred.append(0.0)
else:
grd_num.append(0.0)
cred.append(0.0)
通过检查用户输入是否为空,您可以有效地停止提示输入并用零填充剩余的数据。
推荐阅读
- python - sqlalchemy - 从分区中选择
- c# - 将趋势线添加到图表 /c#
- go - 获取挂载文件夹的路径
- php - 从 php 文件运行 C++ 可执行文件 - 从通过 Web 界面单击的 HTML 按钮调用的 php 文件
- mysql - 如何动态透视mysql表?
- vue.js - Uncaught (in promise) TypeError: Cannot read properties of null (reading 'insertBefore')
- unreal-engine4 - 虚幻引擎编译系统如何工作?
- react-native - FlatList TouchableOpacity 项目看起来被按下
- stored-procedures - 在 UDF 或存储过程中使用参数返回表
- css - CSS Grid - 跨列和行?