當我運行我的代碼,我不明白爲什麼我不斷收到以下錯誤信息:錯誤:錯誤的文件描述符「
with open(classChoice, 'r+') as resultfile:
OSError: [Errno 9] Bad file descriptor
這是我的代碼:
import collections
classChoice = int(input("Which class do you want to order? "))
if classChoice == 1:
open ("Class 1.txt")
elif classChoice == 2:
open ("Class 2.txt")
else:
open ("Class 3.txt")
scores = {}
def alphabetical():
with open(classChoice, 'r+') as resultfile:
for line in resultfile:
name, score = lines.split(":")
scores.setdefault(name, collections.deque(maxlen=3)).append(int(score))
for name in sorted(scores):
m = max(scores[name])
print('{name}: {m}'.format(name=name, m=m))
def highestScore():
with open(classChoice, 'r+') as resultfile:
for line in resultfile:
name, score = line.split(":")
scores.setdefault(name, collections.deque(maxlen=3)).append(int(score))
for name in sorted(scores, key=lambda name: max(scores[name]), reverse=True):
m = max(scores[name])
print('{name}: {m}'.format(name=name, m=m))
def averageScore():
with open(classChoice, 'r+') as resultfile:
d = {}
for line in resultfile:
column = line.split(":")
names = column[0]
scores = int(column[1].strip())
d.setdefault(names, []).append(scores)
averages=[]
for names, v in d.items():
average = (sum(v[-3:])/len(v[-3:]))
averages.append((names, average))
for names, average in sorted(averages, key=lambda a: a[1], reverse=True):
print(names, average)
orderChoice = (input("How do you want to order the data? "))
if orderChoice == "alphabetically":
alphabetical()
elif orderChoice == "with highest score for the tests, highest to lowest":
highestScore()
else:
averageScore()
因爲你試圖打開一個數字,所以'如果classChoice == 1:open(「Class 1.txt」)'不會做什麼你試圖做... –