为什么这个函数addStuent()没有返回我在python中的期望

时间:2018-03-08 07:18:06

标签: python class oop

我有一个名为student.py的python类,其中包含属性和方法,其中一种方法称为 addStudent(self,name,age)。它需要2个参数。

问题是这个函数使用setter方法显示新学生的姓名和年龄。

但控制台会显示:

没有新生,他/她没有年龄

其中无必须是名称和年龄

student.py

class student():
    def __init__(self,name,age, grade):
        self.stdName=name
        self.stdAge = age
        self.stdGrade = grade

    def getName(self):
        print("the student name {}".format(self.stdName))

    def setName(self,Name):
        self.stdName = Name

    def getAge(self):
        print("the student age :{}".format(self.stdAge))

    def setAge(self, Age):
        self.stdAge = Age

    def getGrade(self):
        print("the student {0}, have a grade {1}".format(self.stdName,self.stdGrade))

        if self.stdGrade<2.5:
            print("you failed")
        elif self.stdGrade>2.5:
            print("good job continue")
        elif self.stdGrade == 5.0:
            print("you are a honor student")

    def setGrade(self,Grade):
        self.stdGrade = Grade


    def getTheAVG(self,initial,*grades):

        value = initial
        for item in grades:
            value+=float(item)
            return value/(len(grades)+1)

        print("Average of the student is " + str((grades)))

    def addStudent(self,name,age):
        print("{0} is a new student and he/she has {1} years old"
              .format(self.setName(name),self.setAge(age)))


std1 = student("georges", 17, 3.4)
std1.getName()
std1.getAge()
std1.getGrade()
std1.addStudent("jiji",12)

std1.getTheAVG(2.4,5.0,4.6,2.2,1.2)

4 个答案:

答案 0 :(得分:2)

你的功能没有返回一个值,因此得到None。你可以这样做。

def setAge(self, Age):
        self.stdAge = Age
        return self.stdAge

但是,从设计的角度说它是一个setter方法,你不会期望setter函数返回任何值

答案 1 :(得分:1)

您的print依赖于两种不返回任何内容的方法的返回值。要么setNamesetAge方法返回它们设置的值,要么只使用属性值。

答案 2 :(得分:0)

检查一下:

class student():
    def __init__(self,name,age, grade):
        self.stdName=name
        self.stdAge = age
        self.stdGrade = grade

    def getName(self):
        print("the student name {}".format(self.stdName))

    def setName(self,Name):
        self.stdName = Name
        return self.stdName

    def getAge(self):
        print("the student age :{}".format(self.stdAge))

    def setAge(self, Age):
        self.stdAge = Age
        return self.stdAge

    def getGrade(self):
        print("the student {0}, have a grade {1}".format(self.stdName,self.stdGrade))

        if self.stdGrade<2.5:
            print("you failed")
        elif self.stdGrade>2.5:
            print("good job continue")
        elif self.stdGrade == 5.0:
            print("you are a honor student")

    def setGrade(self,Grade):
        self.stdGrade = Grade


    def getTheAVG(self,initial,*grades):

        value = initial
        for item in grades:
            value+=float(item)
            return value/(len(grades)+1)

        print("Average of the student is " + str((grades)))

    def addStudent(self, name, age):
        print("{0} is a new student and he/she has {1} years old"
              .format(self.setName(name), self.setAge(age)))


std1 = student("georges", 17, 3.4)
std1.getName()
std1.getAge()
std1.getGrade()
std1.addStudent("jiji",12)

std1.getTheAVG(2.4,5.0,4.6,2.2,1.2)

这将为您提供所需的输出。

注意:您的函数没有返回给您None的方式的值,但从设计的角度来看,因为您的方法是setter方法,而setter方法并非如此。 t返回一个值。

答案 3 :(得分:0)

如果您希望您的setter方法返回某些内容,请执行以下操作

class Student:

def setName(self,Name):
    self.stdName = Name
    return self.stdName

def setAge(self, Age):
    self.stdAge = Age
    return self.stdAge