def main(): """Run the digital clock program.""" while True: # Main program loop. # Clear the screen: if sys.platform == 'win32': os.system('cls') # Clears Windows terminal. else: os.system('clear') # Clears macOS/Linux terminal. # Get the current time from the computer's clock: currentTime = time.localtime() # % 12 so we use a 12-hour clock, not 24: hours = str(currentTime.tm_hour % 12) if hours == '0': hours = '12' # 12-hour clocks show 12:00, not 00:00. minutes = str(currentTime.tm_min) seconds = str(currentTime.tm_sec) # Pad these strings with zeros, if needed: hours = hours.zfill(2) minutes = minutes.zfill(2) seconds = seconds.zfill(2) # Get the digit strings from the sevseg module: hDigits = sevseg.getSevSegStr(hours) hTopRow, hMiddleRow, hBottomRow = hDigits.splitlines() mDigits = sevseg.getSevSegStr(minutes) mTopRow, mMiddleRow, mBottomRow = mDigits.splitlines() sDigits = sevseg.getSevSegStr(seconds) sTopRow, sMiddleRow, sBottomRow = sDigits.splitlines() # Display the digits: print(hTopRow + ' ' + mTopRow + ' ' + sTopRow) print(hMiddleRow + ' * ' + mMiddleRow + ' * ' + sMiddleRow) print(hBottomRow + ' * ' + mBottomRow + ' * ' + sBottomRow) print() print('Press Ctrl-C to quit.') # Keep looping until the second changes: while True: time.sleep(0.01) if time.localtime().tm_sec != currentTime.tm_sec: break
import sys, time import sevseg secondsLeft = int(input("Enter how many seconds are left: ")) try: while True: print('\n' * 60) hours = str(secondsLeft // 3600) minutes = str((secondsLeft % 3600) // 60) seconds = str(secondsLeft % 60) hDigits = sevseg.getSevSegStr(hours, 2) hTopRow, hMiddleRow, hBottomRow = hDigits.splitlines() mDigits = sevseg.getSevSegStr(minutes, 2) mTopRow, mMiddleRow, mBottomRow = mDigits.splitlines() sDigits = sevseg.getSevSegStr(seconds, 2) sTopRow, sMiddleRow, sBottomRow = sDigits.splitlines() print(hTopRow + ' ' + mTopRow + ' ' + sTopRow) print(hMiddleRow + ' ' + mMiddleRow + ' ' + sMiddleRow) print(hBottomRow + ' ' + mBottomRow + ' ' + sBottomRow) if secondsLeft == 0: print() print(' ****BOOM****') break
# Get the hours/minutes/seconds from secondsLeft: # For example: 7265 is 2 hours, 1 minute, 5 seconds. # So 7265 // 3600 is 2 hours: h = str(secondsLeft // 3600) # And 7265 % 3600 is 65, and 65 // 60 is 1 minute: m = str((secondsLeft % 3600) // 60) # And 7265 % 60 is 5 seconds: s = str(secondsLeft % 60) # Pad these strings to two digits with zeros, if needed: h = h.zfill(2) # h[0] is the first digit, h[1] is the second digit m = m.zfill(2) # m[0] is the first digit, m[1] is the second digit s = s.zfill(2) # s[0] is the first digit, s[1] is the second digit # Get the digit strings from the sevseg module: hDigits = sevseg.getSevSegStr(h) hTopRow, hMiddleRow, hBottomRow = hDigits.splitlines() mDigits = sevseg.getSevSegStr(m) mTopRow, mMiddleRow, mBottomRow = mDigits.splitlines() sDigits = sevseg.getSevSegStr(s) sTopRow, sMiddleRow, sBottomRow = sDigits.splitlines() # Display the digits: print(hTopRow + ' ' + mTopRow + ' ' + sTopRow) print(hMiddleRow + ' * ' + mMiddleRow + ' * ' + sMiddleRow) print(hBottomRow + ' * ' + mBottomRow + ' * ' + sBottomRow) print() # Print a newline. if secondsLeft == 0: