Python创建一个程序,用于执行和分析课程的最终成绩
下面是我打算做的 创建一个程序,用于执行和分析课程的最终成绩。程序必须使用循环,并在添加每个等级时将其附加到列表中。该程序要求用户输入10名学生的最终分数(分数百分比为整数)。然后,程序将显示以下数据:Python创建一个程序,用于执行和分析课程的最终成绩,python,list,append,average,min,Python,List,Append,Average,Min,下面是我打算做的 创建一个程序,用于执行和分析课程的最终成绩。程序必须使用循环,并在添加每个等级时将其附加到列表中。该程序要求用户输入10名学生的最终分数(分数百分比为整数)。然后,程序将显示以下数据: 全班得分最高 全班最低的分数 班上的平均分数 我一直在第12行遇到一个错误,无法找出原因 错误: Traceback (most recent call last): File "H:/COMS-170/program7.py", line 33, in <module>
- 全班得分最高
- 全班最低的分数
- 班上的平均分数李>
Traceback (most recent call last):
File "H:/COMS-170/program7.py", line 33, in <module>
main()
File "H:/COMS-170/program7.py", line 12, in main
total = sum(info)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
当用户输入返回
str
值时,您的解决方案需要稍加修改,您希望sum
这些值,但首先将它们转换为int
s,如下所示:
def main():
info = get_values()
total = sum(info)
average = total/len(info)
print('Highest Grade: ', max(info))
print('Lowest Grade: ', min(info))
print('Average is: ', average)
def get_values():
num_grades = 10
#making of the list
grades = []
#ask the user for the info
print('Please enter the final grades for 10 students: ')
#put the info into the list with a loop
for i in range(num_grades):
grade = int(input('Enter a grade: ')) # convert the input `str` to `int`
grades.append(grade)
return grades
main()
此外,在int
转换过程中,还应注意不要出现异常,如ValueError
希望有帮助 问题是,读取输入后,将在变量grades中获得字符串列表。 因此,您可以使用int方法解析输入:
grades.append(int(grade))
你犯了什么错误。是“意外缩进”错误吗?第12行之后的所有内容似乎都使用了3个空格,而不是4个空格。@TehTris否这是一个回溯(最近一次调用):文件“H:/COMS-170/program7.py”,第33行,在main()文件“H:/COMS-170/program7.py”的第12行,在main total=sum(info)TypeError:不支持的+操作数类型:'int'和'str',欢迎使用。如果需要处理浮动,原理是相同的
grades.append(int(grade))