好的,我需要這個問題的幫助,這是我上一個大學的項目,我不是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'
>>>