Q:

Python program to find elder of two persons

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now