Python program to find elder of two persons

Here, we are going to write a Python program to find the elder person of two persons using class & object.
Submitted by Shivang Yadav, on February 13, 2021

Problem Statement: Python program to find the elder person of two persons.

Problem Description: We need to get input from users for the name and age of two persons. And then return the elder person's name and age.

Steps to find the sum of distances:

  • Step 1: Create a class named Person with methods to get data, print data and return the older person's details.
  • Step 2: GetPerson() method takes input from users i.e. name and age of person.
  • Step 3: PutPerson() method prints the data of the object.
  • Step 4: For the person's information, compare their ages and return the person's information with more age.

Program to illustrate the program

class Person:
    def GetPerson(self):
        self.__name=input("Enter Name: ")
        self.__age=int(input("Enter Age: "))
    def PutPerson(self):
        print(self.__name,self.__age)

    def __gt__(self, T):
      if (self.__age > T.__age):
        return True
      else:
        return False

    def __lt__(self, T):
        if (self.__age < T.__age):
            return True
        else:
            return False


P1=Person()
P2=Person()

P1.GetPerson()
P2.GetPerson()


if(P1>P2):
    print("Elder Person: ")
    P1.PutPerson()
elif(P1<P2):
    print("Elder Person: ")
    P2.PutPerson()
else:
    print("Both Are Equal: ")
    P1.PutPerson()
    P2.PutPerson()

Output:

Run 1:
Enter Name: Shivang
Enter Age: 21
Enter Name: Prem
Enter Age: 28
Elder Person:
Prem 28

Run 2:
Enter Name: Mini Bhatia
Enter Age: 35
Enter Name: Micky
Enter Age: 21
Elder Person:
Mini Bhatia 35

Run 3:
Enter Name: Prem
Enter Age: 28
Enter Name: Radib
Enter Age: 28
Both Are Equal:
Prem 28
Radib 28

Python class & object programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.