Define a class StudentDefine __init__ method with arguments self, name and age.Inside the __init__ method, set self.name = name, self.age = age.Take name and age from the console by calling input and store them in the variables s1_name, s1_ageCreate an instance Stud_1 of class Student by passing the values as argumentsTake name and age from the console by calling input and store them in the variables s2_name, s2_ageCreate an instance Stud_2 of class Student by passing the values as argumentsPrint Stud_1.namePrint Stud_2.name
Question
Define a class StudentDefine init method with arguments self, name and age.Inside the init method, set self.name = name, self.age = age.Take name and age from the console by calling input and store them in the variables s1_name, s1_ageCreate an instance Stud_1 of class Student by passing the values as argumentsTake name and age from the console by calling input and store them in the variables s2_name, s2_ageCreate an instance Stud_2 of class Student by passing the values as argumentsPrint Stud_1.namePrint Stud_2.name
Solution
Here is the Python code that follows your instructions:
# Step 1: Define a class Student
class Student:
# Step 2: Define __init__ method with arguments self, name and age.
def __init__(self, name, age):
# Step 3: Inside the __init__ method, set self.name = name, self.age = age.
self.name = name
self.age = age
# Step 4: Take name and age from the console by calling input and store them in the variables s1_name, s1_age
s1_name = input("Enter the name of the first student: ")
s1_age = input("Enter the age of the first student: ")
# Step 5: Create an instance Stud_1 of class Student by passing the values as arguments
Stud_1 = Student(s1_name, s1_age)
# Step 6: Take name and age from the console by calling input and store them in the variables s2_name, s2_age
s2_name = input("Enter the name of the second student: ")
s2_age = input("Enter the age of the second student: ")
# Step 7: Create an instance Stud_2 of class Student by passing the values as arguments
Stud_2 = Student(s2_name, s2_age)
# Step 8: Print Stud_1.name
print(Stud_1.name)
# Step 9: Print Stud_2.name
print(Stud_2.name)
This code will create a class Student with an __init__ method that takes name and age as parameters. It will then create two instances of this class, Stud_1 and Stud_2, using input from the console. Finally, it will print the names of these two instances.
Similar Questions
Create a base class Person and a derived class Student with Person as its base class.Add two methods setname() (which takes the parameter self and name)and getname() which prints the name in the base class.Add two methods in the derived class: setage() (which takes the parameters self and age) which sets the age and getage() which prints the age.Create an instance of Student and name it as s1.Take name and age as inputs from the console.Call the setname() and setage() on this instance by passing the name and age parameters.Call the getname() and getage() on this class, which prints the passed parametersNotice that we have created an object of derived class but it could access the methods of base class.This is inheritance.
class Student: def __init__(self): self.name = name self.studentID = studentID def returnName (self): return self.name def returnAge(self): return self.age def setAge(self,age): self.age = agedef main(): n = input("what is the student's name? ") listOfStudents = [] while (n): i = int (input ("what is the student's ID number? ")) myStudent = Student(n, i) listOfStudents.append(myStudent) print (myStudent.returnName()) myStudent.setAge (20) n = input("what is the student's name? ") for student1 in listOfStudents: print (student1.returnName(), student1.returnAge())main()
Student class is defined with public variables name, age, group and reportDefine setDetails() method that sets the student name, age, group and report values.Define getDetails(self) method that prints all the details of the student.Create an instance of class StudentSet the name, age, group and report values of the student by taking the input from the student and call the setDetails() valuesNow print the details of Student by calling the method showDetails()Sample Test Cases
class Student: def __init__(self,name,id): self.name = name self.id = id print(self.id, end=" ")std1=Student("Mark",1)std2=Student("May",2)std2.id=2print(std2.id, end=" ")
Create a class named 'Student' with a string variable 'name' and an integer variable 'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.