Class Student (object):
def __init__ (self,name= "", school= "", Grade= ""):
If not name:
Name=raw_input ("What's the student ' s name?")
If not school:
School=raw_input ("What's the student ' s school?")
If not grade:
Grade=self.get_grade ()
Self.name=name
Self.school=school
Self.grade=grade
Self.print_student ()
def get_grade (self):
While True:
Grade=raw_input ("What's the student ' s grade?[ K,1-5] ")
If Grade.lower () not in [' K ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']:
Print "I ' m sorry, but {} isn ' t valid." Format (grade)
Else
Return grade
def print_student (self):
Print "Name: {}". Format (Self.name)
Print "School: {}". Format (Self.school)
Print "Grade: {}". Format (Self.grade)
def main ():
Student1=student ()
Student2=student (name= "Harry", grade= "2", school= "Minnieville")
If __name__== "__main__":
Main ()
Note: 1. You must have the parameter object in the Created class
2. "! "To become Chinese symbol ' Hello world\xa3\xa1 ' may appear error
3. Methods in a class pass in a value that must have self, in the method body reference property
4. When calling this class, do not use the duplicate object
5.__name__, __main__, and __init__ are double-underlined
The 6.python comes with the initialization method __init__ (), which is called automatically when the class is called
Python: Creating an instance of a class