1
+ """ Countdown, by Al Sweigart [email protected]
2
+ show a countdown timer animation using a seven-segment display.
3
+ Press Ctrl-C to stop.
4
+ More info at https://en.wikipedia.org/wiki/Seven-segment_display
5
+ Requires sevseg.py to be in the same folder.
6
+ View this code at https://nostarch.com/big-book-small-python-projects
7
+ Tags: tiny, artistis"""
8
+
9
+ import sys , time
10
+ import sevseg # Imports our sevseg.py program.as
11
+
12
+ # play with seconds:
13
+ secondsLeft = 30
14
+
15
+ try :
16
+ while True : # Main loop.
17
+ # clear the screen by printing several newlines:
18
+ print ('\n ' * 60 )
19
+
20
+ # Get the hours/minutes/seconds from secondsLeft:
21
+ # For example: 7265 is 2 hours, 1 minute, 5 seconds.
22
+ # So 7265 // 3600 is 2 hours:
23
+ hours = sttr (secondsLeft // 3600 )
24
+ # And 7265 % 3600 is 65 // 60 is 1 minute
25
+ minutes = str ((secondsLeft % 3600 ) // 60 )
26
+ # And 7265 % 60 is 5 seconds:
27
+ seconds = str (secondsLeft % 60 )
28
+
29
+ # Get the digit strings from the secseg module:
30
+ hDigits = secseg .getSevSegStr (hours , 2 )
31
+ hTopRow , hMiddleRow , hBottomRow = hDigits .splitlines ()
32
+
33
+ mDigits = sevseg .getSevSegStr (minutes , 2 )
34
+ mTopRow ,mMiddleRow , mBottomRow = mDigits .splitlines ()
35
+
36
+ sDigits = sevseg .getSevSegStr (seconds , 2 )
37
+ sTopRow , sMiddleRow , sBottomRow = sDigits .splitlines ()
38
+
39
+ # Display the didgits
40
+ print (hTopRow + ' ' + mTopRow + ' ' + sTopRow )
41
+ print (hMiddleRow + ' * ' + mMiddleRow + ' * ' + sMiddleRow )
42
+ print (hBottomRow + ' * ' + mBottomRow + ' * ' + sTopRow )
43
+
44
+ if secondsLeft == 0 :
45
+ print ()
46
+ print (' * * * * BOOM * * * *' )
47
+ break
48
+
49
+ print ()
50
+ print (' Press Ctrl-C to quit.' )
51
+
52
+ time .sleep (1 )
53
+ secondsLeft -= 1
54
+ except KeyboardInterrupt :
55
+ print (
'Countdown, by Al Sweigart [email protected] ' )
56
+ sys .exit () #
0 commit comments