Apurva Patel Apurva Patel - 11 days ago 11
Python Question

Python Clock Class

class Clock:
def __init__(self, hrsIn, minsIn, secsIn): # to create the clock
self.hours = hrsIn
self.minutes = minsIn
self.seconds = secsIn

def SetTime(self, newHrsIn, newMinsIn, newSecsIn):
SetTime = self.hours
SetTime = self.minutes
SetTime = self.seconds

def GetHours(self):
return self.hours

def GetMinutes(self):
return self.minutes

def GetSeconds(self):
return self.seconds

def DisplayTime24(self):
print("The time is",format(self.hours,"1d"),":",end=" ")
if (self.minutes < 10):
print("0",end="")
print(format(self.minutes,"1d"),": ", end="")
else:
print(format(self.minutes, "1d"),": ", end="")
if (self.seconds < 10):
print("0",end="")
print(format(self.seconds, "1d"),end="")
else:
print(format(self.seconds, "1d"),end="")
print()


def DisplayTime12(self):
print("The time is ",end="")
if (self.hours >= 12):
t = self.hours - 12
print(format(t,"2d"),": ",end="")
if (self.minutes < 10):
print("0",end="")
print((self.minutes, "1d"),": ", end="")
else:
print(format(self.minutes, "1d"),": ", end="")
if (self.seconds < 10):
print("0",end="")
print(format(self.seconds, "1d"),": ", end="")
else:
print(format(self.seconds, "1d"), end="")
print(" PM")
else:
print(format(self.hours, "1d"), ": ",end="")
if (self.minutes < 10):
print("0",end="")
print(format(self.minutes, "1d"),": ", end="")
else:
print(format(self.minutes, "1d"),": ", end="")
if (self.seconds < 10):
print("0",end="")
print(format(self.seconds, "1d"),end="")
else:
print(format(self.seconds, "1d"),end="")
print(" AM")

# finish the rest of this class method(function) definition

def IncrementClock(self):
self.seconds = self.seconds + 1
if (self.seconds == 60):
self.seconds = 0
self.minutes = self.minutes + 1
if (self.minutes == 60):
self.minutes = 0
self.hours = self.hours + 1
if (self.hours == 60):
self.hours = 0
else:
print()
# finish the rest of this class method(function) definition

def main():
myClock = Clock(0,0,0) # create a clock with a time of midnight
myClock.DisplayTime12() # display it with a 12-hour format
myClock.DisplayTime24() # display it with a 24-hour format
print()

myClock.SetTime(22,30,5) # change the time to 10:30:05 PM
myClock.DisplayTime12() # display it with a 12-hour format
myClock.DisplayTime24() # display it with a 24-hour format
print()

myClock.SetTime(23,59,59) # change the time to 11:59:59
myClock.DisplayTime12() # display it with a 12-hour format
myClock.DisplayTime24() # display it with a 24-hour format
print()
myClock.IncrementClock() # increment the clock
myClock.DisplayTime12() # display new/current time in 12-hour format
myClock.DisplayTime24() # display new/current time in 24-hour format
print()

main()


Any idea where I might be going wrong? It's printing 0's for all. I have no idea how my SetTime function should have in it but this is what it's giving out to me.

The time is 0 : 00 : 00 AM
The time is 0 : 00 : 00

The time is 0 : 00 : 00 AM
The time is 0 : 00 : 00

The time is 0 : 00 : 00 AM
The time is 0 : 00 : 00

The time is 0 : 00 : 01 AM
The time is 0 : 00 : 01


This is what it's supposed to look like.

The time is 12: 00: 00 AM
The time is 0: 00: 00

The time is 10: 30: 05 PM
The time is 22: 30: 05

The time is 11: 59: 59 AM
The time is 11: 59: 59

The time is 12: 00: 00 PM
The time is 12: 00: 00

Answer

Just change SetTime to assign the parameters to member variables like in __init__ just above it:

def SetTime(self, newHrsIn, newMinsIn, newSecsIn):
    self.hours = newHrsIn
    self.minutes = newMinsIn
    self.seconds = newSecsIn

What you have now just assigns member variables to a variable called SetTime which is then discarded.