我可以在对象内创建一个列表

时间:2017-03-23 03:20:30

标签: python

我是一名刚进入python的中级程序员。我正在创建一个简单的课堂管理器程序,允许您创建一个具有属性名称ageavg的学生。我想知道我是否有可能创建一个函数,即getMarks(),允许我为该学生插入x个标记,并将该列表存储在属性avg中(或实际平均值sum(list)/len(list)

以下是一些代码示例,以便您了解我的意思更好

class Student():

    def __init__(self, name, grade, avg):
        self.name = name
        self.grade = grade
        self.avg = avg

    def addStudent():

        studentNum = int(input("How many students do you want to add?: "))

        for n in range(studentNum):

            newName = input("Student Name: ")
            newGrade = int(input("Student Grade: "))
            newAvg = random.randrange(50, 100) 
            # *i would like to be able to store this as a list*
            myClass.append(Student(newName, newGrade, newAvg))

这里有一些我尝试过但没有一个真正起作用的东西

def setMark():

    markWho = input("Who are you adding a mark(s) for?: ")
    newMark = int(input("Enter mark: "))


    for i in myClass:
        if(i.name == markWho):
            markDict[i] = []

            markDict[i].append(newMark)

def getMark():
    markWho2 = input("Whose mark's would you like to see?: ")

    studAvg = 0

    for i in myClass:
        if(i.name == markWho2):
            studAvg = sum(markDict[i]) / len(markDict[i])
            print(markDict[i] + " \nAccording to this data this student has a {}%       average".format(studAvg))

def getAvg(name):
    for i in markDict:
        if(i.name == name):
        print(markDict[i])

2 个答案:

答案 0 :(得分:2)

您可以在Student类中添加属性markDict,这样更容易管理(因为您正在处理OOP)。这个属性将是一个列表。

这是一种方法

 <uses-permission android:name="android.permission.WAKE_LOCK"/>
 <receiver android:name=".services.AlarmReceiver"/>

以下是一些输出

import random
myClass=[]
class Student(object):
    def __init__(self, name, grade, avg):
        self.name = name
        self.grade = grade
        self.avg = avg
        #add an attribute for each student class. This attribute is a list
        self.markDict=[]

def addStudent():
    studentNum = int(input("How many students do you want to add?: "))
    for n in range(studentNum):
        newName = input("Student Name: ")
        newGrade = int(input("Student Grade: "))

def setMark():
    markWho = input("Who are you adding a mark(s) for?: ")
    newMark = int(input("Enter mark: "))
    for i in myClass:
        if(i.name == markWho):
            #add this mark to the markDict attribute list for that Student object
            i.markDict.append(newMark)

def getMark():
    markWho2 = input("Whose mark's would you like to see?: ")
    for i in myClass:
        if(i.name == markWho2):

            studAvg = sum(i.markDict) / (len(i.markDict) if len(i.markDict)!=0 else 1)
            print("nAccording to this data this student has a {}%       average".format(studAvg))

def getAvg(name):
    for i in markDict:
        if(i.name == name):
            print(i.markDict)

addStudent()
getMark()
setMark()
getMark()

答案 1 :(得分:0)

另一种方法:

import random 

class Student():

    def __init__(self, name, grade, avg):
        self.name = name
        self.grade = grade
        self.avg = avg
        self.markList = []

    def getAvg(self):
        if len(self.markList) > 0:
            self.avg = sum(self.markList) / len(self.markList)

    def addMark(self,newMarks):
        self.markList.extend(newMarks)

def addStudent():
    studentNum = int(input("How many students do you want to add?: "))
    for n in range(studentNum):
        newName = input("Student Name: ")
        newGrade = int(input("Student Grade: "))
        newAvg = random.randrange(50, 100) 
        myClass.append(Student(newName, newGrade, newAvg))

def setMark():
    markWho = input("Who are you adding a mark(s) for?: ")
    iterMark = int(input("How many marks of student you want to add?:"))
    marks = []
    for i in myClass:
       if (i.name == markWho):
           for item in range(iterMark):
               newMark = int(input("Enter mark: "))
               marks.append(newMark)
           i.addMark(marks)
           i.getAvg()
           break

def getMark():
    markWho2 = input("Whose mark's would you like to see?: ")
    for i in myClass:
        if(i.name == markWho2):
            print(i.markList)
            print(" \nAccording to above data this student has a {}%
    average".format(i.avg))

myClass = []
addStudent()
setMark()
getMark()