请告诉我为什么我的输出中没有“ none”

时间:2018-06-25 07:14:35

标签: python python-3.x

大家好,我实际上是用Python开发的程序,它工作正常,但问题是输出。列表中总是有一个“无”的值,我知道这不是一个大问题,因为它可以工作,但是我不能帮我这个事实,即我的系统中有些东西我不了解,这就是“无”的显示即使我也不想这样做。我是初学者。所以先谢谢。

class SchoolMember:

    def __init__(self,position,name,age):
        self.position = position
        self.name = name
        self.age = age

        print("Adding {} with {} of age as a {}".format(self.name, self.age, self.position))

    def tell(self):

        print("Position: {} | Name: {} | Age: {}".format(self.position, self.name,self.age))

class Teacher:

    def __init__(self,position,name,age,salary):

        SchoolMember.__init__(self,position,name,age)
        self.salary = salary

        print("Adding {} with an age of {} and a salary of {} as a {}".format(self.name,self.age,self.salary,self.position))

    def tell(self):

        SchoolMember.tell(self)
        print("Salary: {}".format(self.salary))

class Student:

    def __init__(self,position,name,age,marks):

        SchoolMember.__init__(self,position,name,age)
        self.marks = marks

        print("Adding {} with an age of {} and a mark of {} as {}".format(self.name, self.age,self.marks,self.position))

    def tell(self):
        SchoolMember.tell(self)
        print("Marks: {}".format(self.marks))



All = []

def addTeach():

    runningT = True

    while runningT:

        teachPos = 'Teacher'
        teachName = input('Enter the name of the Teacher: ')
        teachAge = input('Enter the age of the Teacher: ')
        teachSal = input('Enter the Salary of the Teacher: ')

        teachTemp = Teacher(teachPos, teachName, teachAge, teachSal)

        All.append(teachTemp)

        valY = input('Want to add more Teacher?(y/n): ')

        if valY.lower() == 'y':

            runningT = True

        elif valY.lower() == 'n':

            runningT = False


def addStud():

    runningS = True

    while runningS:

        studPos = 'Student'
        studName = input('Enter the name of the Student: ')
        studAge = input('Enter the age of the Student: ')
        studMarks = input('Enter the Marks of the Student: ')

        studTemp = Student(studPos, studName, studAge, studMarks)

        All.append(studTemp)

        valY = input('Want to add more Student?(y/n): ')

        if valY.lower() == 'y':

            runningS = True

        elif valY.lower() == 'n':

            runningS = False

def show():

    for all in All:

        print(all.tell())


running = True

while running:

    valX = input('Want to add a teacher or a student (s/t/v/n): ')

    if valX.lower() == 't':
        addTeach()

    elif valX.lower() == 's':
        addStud()
    elif valX.lower() == 'v':
        show()
    elif valX.lower() == 'n':
        running = False

输出:

Want to add a teacher or a student (s/t/v/n): t
Enter the name of the Teacher: John Zahmir Olaer
Enter the age of the Teacher: 18
Enter the Salary of the Teacher: 90000
Adding John Zahmir Olaer with 18 of age as a Teacher
Adding John Zahmir Olaer with an age of 18 and a salary of 90000 as a Teacher
Want to add more Teacher?(y/n): y
Enter the name of the Teacher: John Bryan Labe
Enter the age of the Teacher: 17
Enter the Salary of the Teacher: 80000
Adding John Bryan Labe with 17 of age as a Teacher
Adding John Bryan Labe with an age of 17 and a salary of 80000 as a Teacher
Want to add more Teacher?(y/n): n
Want to add a teacher or a student (s/t/v/n): s
Enter the name of the Student: Delfin Razon
Enter the age of the Student: 24
Enter the Marks of the Student: 1.1
Adding Delfin Razon with 24 of age as a Student
Adding Delfin Razon with an age of 24 and a mark of 1.1 as Student
Want to add more Student?(y/n): y
Enter the name of the Student: Robin Bongo
Enter the age of the Student: 17
Enter the Marks of the Student: 2.1
Adding Robin Bongo with 17 of age as a Student
Adding Robin Bongo with an age of 17 and a mark of 2.1 as Student
Want to add more Student?(y/n): n
Want to add a teacher or a student (s/t/v/n): v
Position: Teacher | Name: John Zahmir Olaer | Age: 18
Salary: 90000
None
Position: Teacher | Name: John Bryan Labe | Age: 17
Salary: 80000
None
Position: Student | Name: Delfin Razon | Age: 24
Marks: 1.1
None
Position: Student | Name: Robin Bongo | Age: 17
Marks: 2.1
None
Want to add a teacher or a student (s/t/v/n): n

Process finished with exit code 0

2 个答案:

答案 0 :(得分:2)

这里:

print(all.tell())

您的类tell()方法将打印到stdout并返回None,因此此语句的确会打印None

请注意:您的代码似乎可以正常运行,但这在某种程度上是偶然的-您没有以正确的方式使用继承。您想让TeacherStudent类继承自SchoolMember,并且(最好)使用super()来调用父类方法:

 class Teacher(SchoolMember):
    def __init__(self,position,name,age,salary):
        super().__init__(position,name,age)
        self.salary = salary

        # ...

    def tell(self):
        super().tell()        
        print("Salary: {}".format(self.salary))

答案 1 :(得分:1)

all.tell()打印内容并返回None;然后print(all.tell())打印该None。您应该从tell返回文本,然后从外面打印文本,或者应该在tell中打印,然后进行普通调用,而不是同时进行。