我在运行以下Python脚本时遇到了多个问题。在getStudentGrade函数中,我收到以下错误: ("执行查询&#34后无法设置选项;) pymongo.errors.InvalidOperation:执行查询后无法设置选项
如果我注释掉那个问题的部分(elif语句)我得到了这个错误: TypeError:' NoneType'对象没有属性' getitem '
我认为它与addScore功能有关,但我已经尝试过各种类型的组合而且没有去过。任何帮助表示赞赏。感谢
from pymongo import MongoClient
#Note to run the following code you will need to import the grades.js file
client = MongoClient("localhost", 27017)
db = client['test']
exam_type = u'exam'
quiz_type = u'quiz'
homework_type = u'homework'
####Note: Python enforces space, so ensure that each line of your code is in line with
#### the line above it.
####Note: Only modify this file. Put code in after the line ######### YOUR CODE STARTS HERE ################
def addScore(student_id,score_type,score):
scoreId = None
# Insert a score document into the mod6 collection of the test database.
# Place the id returned from id into the scoreId variable.
######### YOUR CODE STARTS HERE ################
scoreId = db.mod6.insert({"student_id":student_id, "type":score_type, "score":score})
######### YOUR CODE ENDS HERE ################
return scoreId
def updateScore(scoreId,student_id,score_type,score):
result = None
# Update the score document with the "_id" field of scoreId
# , so that it has the student id, score_type, and score from the function arguments.
# Place the id returned from id into the scoreId variable.
######### YOUR CODE STARTS HERE ################
result = db.mod6.update({"_id":scoreId},{"student_id":student_id,"type":score_type,"score":score}, False, False)
######### YOUR CODE ENDS HERE ################
return result
def retrieveOneScore(student_id, score_type):
result = None
# Retrieve one score from the mod6 collection for a specific student with the student id field set
# to the passed student_id argument of the type of the second argument
######### YOUR CODE STARTS HERE ################
results = db.mod6.find_one({"student_id":student_id, "type":score_type})
######### YOUR CODE ENDS HERE ################
return result
def retrieveAllScores(student_id, score_type):
results = None
# Retrieve all scores from the mod6 collection for a specific student with the student id field set
# to the passed student_id argument of the type of the second argument
######### YOUR CODE STARTS HERE ################
results = db.mod6.find({"student_id":student_id, "type":score_type})
######### YOUR CODE ENDS HERE ################
return results
def average(results):
avg = 0
# Average all of the score values in the results variable and place in the avg variable
######### YOUR CODE STARTS HERE ################
num_results = 0
total_score = 0
for result in results:
num_results += 1
total_score += int(result[u'score'])
average_score = total_score/num_results
######### YOUR CODE ENDS HERE ################
return avg
def getStudentGrade(student_id):
result = None
# You will need to retrieve a specific student's (student_id argument) and weight the average
# values for the different grade types (exam 50% of final grade, quiz 30% of final grade,
# homework 20% of final grade) and then add them and place in the result variable
# Note: It may be obvious but the resulting score should be from 0 - 100.
######### YOUR CODE STARTS HERE ################
results = db.mod6.find({"student_id":student_id})
eweight = 50
qweight = 30
hweight = 20
quiztotal = 0
examtotal = 0
homeworktotal = 0
for result in results:
if(result[u'type'] == "quiz"):
quiztotal += int(result[u'score']) * qweight
#elif(results[u'type'] == "exam"):
# examtotal += int(result[u'score']) * eweight
#elif(results[u'type'] == homework):
# homeworktotal += int(result[u'score']) * hweight
######### YOUR CODE ENDS HERE ################
return result
def test_all_methods():
result = []
exam_scores = [82,92]
quiz_scores = [95,72,85]
homework_scores = [35, 90, 78]
student_id = 200
for exam_score in exam_scores:
temp_result = addScore(student_id,exam_type,exam_score)
result.append(temp_result)
for quiz_score in quiz_scores:
temp_result = addScore(student_id,quiz_type,quiz_score)
result.append(temp_result)
for homework_score in homework_scores:
temp_result = addScore(student_id,homework_type,homework_score)
result.append(temp_result)
result.append(retrieveOneScore(student_id, quiz_type))
result.append(retrieveAllScores(student_id, homework_type))
result.append(average(result[9]))
result.append(getStudentGrade(student_id))
return result