Q:

Python program to convert hours into days

belongs to collection: Python class & object programs

0

Class and its members used:

  • Class : time
    • Method : getTime() : takes input for hours, minutes and second from the user.
    • Method : putResult() : converts that time into required format and prints it.
    • Method : __add__() [addition operator ] : adds the respective values for both objects.
    • Variable : d : store the count of days
    • Variable : h : store the count of hours
    • Variable : m : store the count of minutes
    • Variable : s : store the count of seconds

All Answers

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

Program to convert hours into days in Python

class Time:
    def GetTime(self):
        self.__h=int(input("Enter  Hrs: "))
        self.__m = int(input("Enter  Min: "))
        self.__s= int(input("Enter  Second: "))

    def  PutResult(self):
        print("{} Days {} Hours {} Minutes {} Seconds".format(self.__d,self.__h, self.__m, self.__s))
        
    def __add__(self, T):
        R=Time()
        R.__h=self.__h+T.__h
        R.__m=self.__m+T.__m
        R.__s=self.__s+T.__s

        R.__m=R.__m+(R.__s//60)
        R.__s=R.__s%60

        R.__h=R.__h+(R.__m//60)
        R.__m=R.__m%60

        R.__d=R.__h//24
        R.__h=R.__h%24

        return R
        
T1=Time()
T2=Time()

print("Enter value for Time object 1 : ")
T1.GetTime()

print("Enter value for Time object 2 : ")
T2.GetTime()

print("The total time elapsed is ", end = " ")
T3=T1+T2
T3.PutResult()

Output:

Enter value for Time object 1 : 
Enter  Hrs: 54
Enter  Min: 32
Enter  Second: 4
Enter value for Time object 2 : 
Enter  Hrs: 79
Enter  Min: 32
Enter  Second: 5
The total time elapsed is  5 Days 14 Hours 4 Minutes 9 Seconds

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

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to illustrate the working of abstra... >>
<< Python program to illustrate the working of list o...