天天看点

python将成绩写入文件_Python:如何从数字文件(分数)中读取使用作为列表并获得高分,低分和平均分,并将组分成十分位数...

好的,我需要这个问题的帮助,这是我上一个大学的项目,我不是csc专业,所以我真的需要一些帮助!! 我需要从随机数文件中读取并找到max,min和avg。然后将每个(10%)十分位数中的分数从0到100分组。不知道为什么我的代码没有运行,到目前为止我只尝试获得最小值,最大值和平均值。

我已经寻找帮助阅读文件列出问题,但很多解决方案包括某种类型的“带”循环的东西,我还没有学到。此外,考虑可能有单独的功能/方法进行排序,然后是百分比/星星。感谢您的任何帮助!

提示:

我在GottaLearn大学任教的一位同事给我发了一份中期考试的考试成绩列表,该考试成绩是用于使用Python进行入门课程的学生。我们被要求分析分数,如下:识别并打印出最低分,最高分和平均分数。

确定十进制之间有多少分数,即多少分数小于10,多少分数在10到19之间,有多少分数在20到29之间,......,有多少分数在90到99之间,以及如何对于每个计数,打印出范围,该范围内的分数,该范围内总分数中的分数百分比,以及为每个整数打印一个星号的简单直方图。 - 百分比。例如,8.5%将有8个星号。

输出示例:

高分是:100

低分为:0

平均值是:55.49

范围数百分比

=========================================

0-9 75 7.5%*******

10-19 82 8.2%********

等等....

输入文件是.txt文件,每个分数都在新行上。93

46

18

50

98

96

我的代码现在:

def main():#Initialize variables

highScore = 0.00

lowScore = 0.00

avgScore = 0.00

try:

# Open and read file as list

scoresList = open("OldScores.txt", "r" ).readlines()

# Loop to get numbers from file

for line in scoresList:

highScore = max(scoresList)

lowScore = min(scoresList)

avgScore = float(sum(scoresList)/len(scoresList))

# Display

print("The high score is:", highScore)

print("The low score is:", lowScore)

print("The average is:", avgScore)

print("Range\t\tNumber\tPercent\t\t")

print("=========================================")

# Close

scoresList.close()

except ValueError:

print("Sorry, Value Error.")

主要()This is the error that comes out:

Traceback (most recent call last):

File "/Users/meganhorton/Desktop/Python/hw5.py", line 36, in

main()

File "/Users/meganhorton/Desktop/Python/hw5.py", line 20, in main

avgScore = float(sum(scoresList)/len(scoresList))

TypeError: unsupported operand type(s) for +: 'int' and 'str'

>>>