0
class Student(object):

    def__init__(self, name='', school='', grade=''): #This is where I get the error

        if not name:
          name = raw_input('what is the student\'s name: ')
        if not school:
          school = raw_input('What is the studnet\'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 = input('What is the student\'s grade: [K, 1-5]')
            if grade.lower() not in ['k','2','3','4','5']:
                print('I\'m sorry, but {} isn\'t valid.'.format(grade))
            else:
                return grade

    def print_student():
        print('Name: {}'.format(self.name))
        print('School: {}'.format(self.school))
        print('Grade: {}'.format(self.grade))

def main():
    student1 = Student()
    studnet2 = Student(name='Bethmi Amalya', grade = '5', school= 'Visakha Vidyalaya')

if __name__ == '__main__':
    main()
3
  • 3
    Looks like missing space. Try def __init__ instead of def__init__ Commented Apr 21, 2018 at 19:41
  • Thank you very much :) It worked. Im new to programming and thank you so much! Commented Apr 21, 2018 at 19:44
  • no problem. Happy Coding! Commented Apr 21, 2018 at 19:44

2 Answers 2

3

you have 2 issues in code :

1 . def__init__(.... , there should be a space between def keyword and init( i.e def __init__(...

2 . self should be passed to def print_student() i.e print_student(self): and all variable accesses should be with self e.g self.name etc in print_student function .

class Student(object):

    def __init__(self, name='', school='', grade=''): #This is where I get the error

        if not name:
          name = raw_input('what is the student\'s name: ')
        if not school:
          school = raw_input('What is the studnet\'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 is the student\'s grade: [K, 1-5]')
            if grade.lower() not in ['k','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()
    studnet2 = Student(name='Bethmi Amalya', grade = '5', school= 'Visakha Vidyalaya')

if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much :) It worked. still new to prgramming
keep it up :) happy coding
0

In line 1: def__init__(self, name='', school='', grade=''):

There is no space between def and __init__

Add a space to fix the syntax error.

Also:

Python 2 used raw_input()
Python 3 uses input()

Link to Changes in Python 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.