Python Coding Practice Class Method Loops

In this class, We make Python Coding Practice Class Method Loops.

For Complete YouTube Video: Click Here

These examples will help the reader to improve his coding shill.

Take our placement course and complete the practise tests to crack the campus placement exams.

After completing the placement course, the reader can attend any campus placement for service-based companies.

Please solve the examples on your own, then check for a solution.

# find the output of the given code 
class surgeon:
    def __init__(self,surgeonid):
        self.__surgeonid=surgeonid
    def identifyamt(self,feelist,surgeonidlist):
        amount=0.0
        count=0
        for index in range(len(surgeonidlist)):
            if(surgeonidlist[index]==self.__surgeonid):
                amt=feelist[index]
        for index in range(len(surgeonidlist)):
            if feelist[index]<amt:
                count+=1
        return count
surgeonidlist=[1008,1004,1002,1007,1009]
feelist=[18,15,10,12,8]
surgeonobj=surgeon(1004)
print(surgeonobj.identifyamt(feelist,surgeonidlist))

In the above program, the surgeon id list and fee list are given.

The method identify amount will take the surgeon id and take the fee of that surgeon.

The second loop will count the number of surgeons having fee amounts less than the given surgeon id.

Given below is the output of the program.

Output:
3
# A company is trying to conduct 2 competetions to there employees
# They have opened two portals for registration
# identify the code at line1 in the given class below 
# so that the gettotalregistration method should give total no of participants registered for both competetions
class competetion:
    __count1=100
    __count2=500
    def __init__(self,eventname):
        self.eventname=eventname
        self.registrationid=None
        if self.eventname=='D':
            self.registrationid="DC"+str(self.count2method())
        elif self.eventname=='S':
            self.registrationid="SC"+str(self.count1method())
    @classmethod
    def count1method(cls):
        cls.__count1+=1
        return cls.__count1-1
    @classmethod
    def count2method(cls):
        cls.__count2+=1
        return cls.__count2-1
    def gettotalregistration(self):
        --line 1--
obj1=competetion('D')
obj2=competetion('S')
obj2.gettotalregistration()

In the above program, we need to identify the code in the method to get total registration.

The method, get total registration should return the total number of registrations.

We have two class methods.

To learn the basics of class and objects. Click here.

The count1 method will increase the variable count1 by one whenever registration is done on event ‘D.’

The count2 method will increase the variable count2 by one whenever registration is done on event ‘S’.

Initially, the count1 variable is given value 100, and the variable count2 is given value 500.

The variables count1 and count2 are class variables. They are global to all objects.

Whenever an object is created, the count variables are incremented according to the event registration.

To get the count of registrations for event ‘D’. We need to subtract the variable count1 by 100.

The number of registrations for Event ‘S’ will be obtained by subtracting the variable count2 by 500.

Given below is the code to be written at line1.

return ((competetion.__count2-500)+(competetion.__count1-100))