我正在从事一个项目/练习,需要在Python中使用OOP来创建成绩册。我一直在学习Python并使用3.8.3已有大约6周的时间,所以我还是很新。成绩簿有一个基本菜单,您可以在其中添加作业,测验和期末考试成绩。我必须为测验和作业成绩使用一个空列表的类。在阅读了一些有关OOP的内容后,我对代码进行了粗略的草拟,并设法使类在不使用属性和仅使用一种方法的情况下起作用:
class GradeBook:
def main_function():
quiz_scores = []
assignment_scores = []
while True:
try:
quiz_grade = float(input('Please enter a grade for the quiz,or press Enter to stop adding grades: '))
quiz_scores.append(quiz_grade)
except:
break
while True:
try:
assignment_grade = float(input('Please enter a grade for the assignment,or press Enter to stop adding grades: '))
assignment_scores.append(assignment_grade)
except:
break
print (quiz_scores)
print (assignment_scores)
print ('time for totals and averages')
quiz_total = sum(quiz_scores)
assignment_total = sum(assignment_scores)
print ('quiz total ' + str(quiz_total))
print ('assign total ' + str(assignment_total))
if len(quiz_scores) > 0:
quizScoreAverage = sum(quiz_scores)// len(quiz_scores)
else:
quizScoreAverage = 0
if len(assignment_scores) > 0:
assignmentScoreAverage = sum(assignment_scores) // len(assignment_scores)
else:
assignmentScoreAverage = 0
print ('quiz average ' + str(quizScoreAverage))
print ('assign average ' + str(assignmentScoreAverage))
GradeBook.main_function()
这是我遇到问题的地方。我需要将代码分成几种方法/功能,一种用于测验分数,一种用于作业分数,一种将存储最终考试分数而无所作为,另一种用于获取当前的成绩/平均值。我一直在搜寻,但碰壁了。在我尝试将用户输入追加到类中的列表之前,代码将一直工作。再次,这只是代码的粗略草稿,如下所示:
class GradeBook:
# Need this at attribute level for all instances to acccess as there will be an instance the pulls the list to calculate overall grade
assignment_scores = []
# quiz_scores = [] ### - This is the other list that will also be used for the grade
def assignGrade(self,score):
self.score = score
self.assignment_scores.append(score)
#####################################################
'''
This will be a duplicate of the above code but will use values to store quiz grades instead
def quizGrade(self,score):
self.score = score
self.quiz_scores.append(score)
'''
#####################################################
while True:
try:
assignment_grade = float(input('Please enter a grade for the assignment,or press Enter to stop adding grades: '))
# Program works just fine up until this point. My issue is here. Trying to feed the user input into the class instance
# to update the class list that is stored as an attribute. Instead of appending it seems to throw an error,# because it doesn't continue the try loop for input and after the break when the list is printed,no values are shown
assignment_grade = GradeBook.assignGrade(assignment_grade) # <------- THIS IS THE PROBLEM CHILD OF MY CODING
except:
break
#####################################################
''' This block will be used to get input for the quiz grade to append to quiz scores list
while True:
try:
quiz_grade = float(input('Please enter a grade for the assignment,or press Enter to stop adding grades: '))
quiz_grade = GradeBook.quizGrade(quiz_grade) #not sure if this is right?
except:
break
'''
#####################################################
我想我只是不太了解将信息从一个实例发送到另一个实例的整个想法。任何输入,不胜感激。我的计划是,一旦一切都弄清楚,我只需要在此处将代码插入我的最终草案即可:
class GradeBook:
# Initializes empty list to store quiz and assignment grades
quiz_scores = []
assignment_scores = []
#####################################################
def quizScore(self,score)
# lines of code to append user input to quiz list for reference in class
#####################################################
def assignScore(self,score)
# lines of code to append user input to assignment list for reference in class
#####################################################
def finalScore(self,score)
# line of code to store the final exam grade for reference in the class
#####################################################
def currentAverage(self)
if len(self.assignment_scores) > 0:
assignmentScoreAverage = sum(self.assignment_scores) // len(self.assignment_scores)
else:
assignmentScoreAverage = 0
if len(self.quiz_scores) > 0:
quizScoreAverage = sum(self.quiz_scores) // len(self.quiz_scores)
else:
quizScoreAverage = 0
currentGrade = (0.4 * self.final_grade) + (0.3 * quizScoreAverage) + (0.3 * assignmentScoreAverage)
return currentGrade
#####################################################
print('''
Grade Book
0: Exit
1: Enter assignment grade
2: Enter quiz grade
3: Enter final exam grade
4: Display current grade
''')
while True:
try:
selection = int(input('Please enter a choice: '))
if selection == 0:
quit
elif selection == 1:
while True:
try:
assignment_grade = float(input('Please enter a grade for the assignment,or press Enter to stop adding grades: '))
GradeBook.assignScore(assignment_grade)
except:
break
elif selection == 2:
while True:
try:
quiz_grade = float(input('Please enter a grade for the assignment,or press Enter to stop adding grades: '))
GradeBook.quizScore(quiz_grade)
except:
break
elif selection == 3:
while True:
try:
final_grade = float(input('Please enter a grade for the assignment,or press Enter to stop adding grades: '))
GradeBook.finalScore(final_grade)
except:
break
elif selection == 3:
final_grade = float(input('Please enter a grade for the final exam: '))
if isdigit(final_grade)
GradeBook.finalScore(final_grade)
else:
print('Please check your input and try again.')
elif selection == 4:
print(GradeBook.currentAverage())
else:
print('Please check your input and try again.')
continue
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。