Home »
Python »
Python programs
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 »
TOP Interview Coding Problems/Challenges