Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python类给出了;“在ouptut中没有”;_Python_Linux_Class_Constructor - Fatal编程技术网

Python类给出了;“在ouptut中没有”;

Python类给出了;“在ouptut中没有”;,python,linux,class,constructor,Python,Linux,Class,Constructor,我有以下代码: class StudentData: "Contains information of all students" studentNumber = 0; def __init__(self,name,age,marks): self.name = name; self.age = age; self.marks = marks; StudentData.studentNumber += 1;

我有以下代码:

class StudentData:
    "Contains information of all students"
    studentNumber = 0;
    def __init__(self,name,age,marks):
        self.name = name;
        self.age = age;
        self.marks = marks;
        StudentData.studentNumber += 1;
    def displayStudentNumber(self):
        print 'Total Number of students = ',StudentData.studentNumber;
    def displayinfo(self):
        print 'Name of the Student: ',self.name;
        print 'Age of the Student: ', self.age;
        print 'Marks of the Student: ', self.marks;

student1 = StudentData('Ayesha',12,90)
student2 = StudentData('Sarah',13,89)
print "*Student number in case of student 1*\n",student1.displayStudentNumber();
print "Information of the Student",student1.displayinfo();
print "*Student number in case of student 2*\n",student2.displayStudentNumber();
print "Information of the Student",student2.displayinfo();
输出为:

*Student number in case of student 1* Total Number of students = 2 None Information of the Student Name of the Student: Ayesha Age of the Student: 12 Marks of the Student: 90 None *Student number in case of student 2* Total Number of students = 2 None Information of the Student Name of the Student: Sarah Age of the Student: 13 Marks of the Student: 89 None *学生1的学生编号* 学生总数=2 没有一个 学生信息学生姓名:Ayesha 学生年龄:12岁 学生成绩:90分 没有一个 *学生2的学生编号* 学生总数=2 没有一个 学生信息学生姓名:Sarah 学生年龄:13岁 学生成绩:89分 没有一个
我不明白为什么我的输出中会出现这些“无”。有人能解释一下吗?

因为您的函数
displayStudentNumber()
displayinfo()
不返回任何内容

尝试将其更改为:

def displayStudentNumber(self):
    return 'Total Number of students = ' + str(StudentData.studentNumber)

def displayinfo(self):
    print 'Name of the Student: ',self.name;
    print 'Age of the Student: ', self.age;
    print 'Marks of the Student: ', self.marks;
    return ''
由于函数不返回任何内容,因此默认为
None
。这就是它被退回的原因


顺便说一下,python中不需要分号。

因为函数
displayStudentNumber()
displayinfo()
不返回任何内容

尝试将其更改为:

def displayStudentNumber(self):
    return 'Total Number of students = ' + str(StudentData.studentNumber)

def displayinfo(self):
    print 'Name of the Student: ',self.name;
    print 'Age of the Student: ', self.age;
    print 'Marks of the Student: ', self.marks;
    return ''
由于函数不返回任何内容,因此默认为
None
。这就是它被退回的原因


顺便说一下,python中不需要分号。

您应该返回这些字符串,而不是打印它们。没有返回值的函数返回
None
。另外,请不要在Python中使用分号

def displayStudentNumber(self):
      return 'Total Number of students = {0}'.format(StudentData.studentNumber)
def displayinfo(self):
      return '''\
Name of the Student: {0}
Age of the Student: {1}
Marks of the Student {2}'''.format(self.name, self.age, self.marks)

您应该返回这些字符串,而不是打印它们。没有返回值的函数返回
None
。另外,请不要在Python中使用分号

def displayStudentNumber(self):
      return 'Total Number of students = {0}'.format(StudentData.studentNumber)
def displayinfo(self):
      return '''\
Name of the Student: {0}
Age of the Student: {1}
Marks of the Student {2}'''.format(self.name, self.age, self.marks)

您在输出中得到
None
,因为您正在打印调用方法
displaytstudentnumber
的返回值。默认情况下,这将返回
None

您可以打印方法的返回值,也可以只打印。试试这样的

print "Student number in case of student 1"
student1.displayStudentNumber()


您在输出中得到
None
,因为您正在打印调用方法
displaytstudentnumber
的返回值。默认情况下,这将返回
None

您可以打印方法的返回值,也可以只打印。试试这样的

print "Student number in case of student 1"
student1.displayStudentNumber()


@阿耶沙哈桑哈哈,没问题。:)@阿耶沙哈桑哈哈,没问题。:)我对这个也有+1的印象
format()
是一个很好的方法,但对我来说有点复杂。我知道现在我在Python方面很笨:)@AyeshaHassan别担心,我们都是Python的初学者:)我也有+1的经验
format()
是一个很好的方法,但对我来说有点复杂。我知道现在我在Python方面很笨:)@AyeshaHassan别担心,我们都是Python的初学者:)