hi i need someone to tell me why my code is compiling and not printing in shell
ID: 2246655 • Letter: H
Question
hi i need someone to tell me why my code is compiling and not printing in shell
here is the decription of the problem:
Write a class named Student (filename student.py) that holds the following data about a student in attributes:
Name
ID
Classification
Major
GPA
Once you have written the class, write a program (filename hw1.py) that creates four Student objects and then use a set function to change the name of one of your objects. Your program should display the data for each student on the screen.
Properly and adequately comment your code.
EXTRA CREDIT (5pts): Allow your class to accept an empty constructor with default value for your attribute name set to ‘ ‘ .
// studentinfo.py
# define class
class Student:
#"Here i am geting basic information about a student"
def __init__(self,name = None,ID = None,classification=None,major= None,gpa=None):#constructor for name class
if name is None:
self.__name = ''
else:
self.__name = name
if ID is None:
self.__ID = 0
else:
self.__ID = ID
if classification is None:
self.__classification = ''
else:
self.__classification = classification
if major is None:
self.__major = ''
else:
self.__major = major
if gpa is None:
self.__gpa = 0
else:
self.__gpa = gpa
#setter functions
def set_name(self,name): #sets name of person
self.__name = name
def set_Id(self,ID): #sets ID of student
self.__ID = ID
def set_classification(self,classification):#sets classification of student
self.__classification = classification
def set_major(self,major):
self.__major = major
def set_gpa(self,gpa):
self.__gpa = gpa
#getter functions
def get_name(self):
return self.__name
def get_ID(self):
return self.__ID
def get_classification(self):
return self.__classification
def get_major(self):
return self.__major
def get_gpa(self):
return self.__gpa
// if i dont use arguments for my get functions it says i need them, it also says my constructor cannot construct over 5 things, but i have 6.
// mainstudent.py.
import studentinfo
def main():
## studentA = jada.Student('Jayla',900745231,'junior','computer science',3.3)
## print(studentA.get_name())
## StudentB = studentinfo.Student()
# print(StudentB.get_name())
# print(StudentB.getID())
# StudentB.set_name('Karen')
# print(StudentB.get_name())
# main()
ob1=studentinfo.Student()
ob1.set_name('Joey')
ob1.set_ID('123456')
ob1.set_classification('Boy')
ob1.set_major('Computer Science')
ob1.set_gpa(7.86)
print(ob1.get_name())
print(ob1.get_ID())
print(ob1.get_classification())
print(ob1.get_major())
print(ob1.get_gpa())
ob2 = studentinfo.Student()
ob2.set_name('Chandler')
ob2.set_ID('123457')
ob2.set_classification('Boy')
ob2.set_major('Computer Science')
ob2.set_gpa(7.86)
print()
print(ob2.get_name())
print(ob2.get_ID())
print(ob2.get_classification())
print(ob2.get_major())
print(ob2.get_gpa())
ob3 = studentinfo.Student()
ob3.set_name('Ross')
ob3.set_ID('123458')
ob3.set_classification('Boy')
ob3.set_major('Computer Science')
ob3.set_gpa(7.86)
print()
print(ob3.get_name())
print(ob3.get_ID())
print(ob3.get_classification())
print(ob3.get_major())
print(ob3.get_gpa())
ob4 = studentinfo.Student()
ob4.set_name('Mathew')
ob4.set_ID('123459')
ob4.set_classification('Boy')
ob4.set_major('Computer Science')
ob4.set_gpa(7.86)
print()
print(ob4.get_name())
print(ob4.get_ID())
print(ob4.get_classification())
print(ob4.get_major())
print(ob4.get_gpa())
main()
//
Explanation / Answer
#The program given in the question is fine. Some minor modification regarding indentation #for main(main has been given the same indentation as if it is a part of def main: and #hence there was no call to main() in the program and hence no prints on the output) is #done and the function name set_ID has been corrected to set_Id. Rest of all is fine. The #program is printing data on the screen.
#studentinfo.py
# define class
class Student:
#"Here i am geting basic information about a student"
def __init__(self,name = None,ID = None,classification=None,major= None,gpa=None):#constructor for name class
if name is None:
self.__name = ''
else:
self.__name = name
if ID is None:
self.__ID = 0
else:
self.__ID = ID
if classification is None:
self.__classification = ''
else:
self.__classification = classification
if major is None:
self.__major = ''
else:
self.__major = major
if gpa is None:
self.__gpa = 0
else:
self.__gpa = gpa
#setter functions
def set_name(self,name): #sets name of person
self.__name = name
def set_Id(self,ID): #sets ID of student
self.__ID = ID
def set_classification(self,classification):#sets classification of student
self.__classification = classification
def set_major(self,major):
self.__major = major
def set_gpa(self,gpa):
self.__gpa = gpa
#getter functions
def get_name(self):
return self.__name
def get_ID(self):
return self.__ID
def get_classification(self):
return self.__classification
def get_major(self):
return self.__major
def get_gpa(self):
return self.__gpa
mainstudent.py
import studentinfo
def main():
ob1=studentinfo.Student()
ob1.set_name('Joey')
ob1.set_Id('123456')
ob1.set_classification('Boy')
ob1.set_major('Computer Science')
ob1.set_gpa(7.86)
print(ob1.get_name())
print(ob1.get_ID())
print(ob1.get_classification())
print(ob1.get_major())
print(ob1.get_gpa())
ob2 = studentinfo.Student()
ob2.set_name('Chandler')
ob2.set_Id('123457')
ob2.set_classification('Boy')
ob2.set_major('Computer Science')
ob2.set_gpa(7.86)
print()
print(ob2.get_name())
print(ob2.get_ID())
print(ob2.get_classification())
print(ob2.get_major())
print(ob2.get_gpa())
ob3 = studentinfo.Student()
ob3.set_name('Ross')
ob3.set_Id('123458')
ob3.set_classification('Boy')
ob3.set_major('Computer Science')
ob3.set_gpa(7.86)
print()
print(ob3.get_name())
print(ob3.get_ID())
print(ob3.get_classification())
print(ob3.get_major())
print(ob3.get_gpa())
ob4 = studentinfo.Student()
ob4.set_name('Mathew')
ob4.set_Id('123459')
ob4.set_classification('Boy')
ob4.set_major('Computer Science')
ob4.set_gpa(7.86)
print()
print(ob4.get_name())
print(ob4.get_ID())
print(ob4.get_classification())
print(ob4.get_major())
print(ob4.get_gpa())
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.