A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://inventwithpython.com/bigbookpython/project14.html below:

Countdown

#14
Countdown

This program displays a digital timer that counts down to zero. Rather than render numeric characters directly, the sevseg.py module from Project 64, “Seven-Segment Display Module,” generates the drawings for each digit. You must create this file before the Countdown program can work. Then, set the countdown timer to any number of seconds, minutes, and hours you like. This program is similar to Project 19, “Digital Clock.”

The Program in Action

When you run countdown.py, the output will look like this:

 __   __       __   __       __   __
|  | |  |  *  |  | |  |  *   __| |__|
|__| |__|  *  |__| |__|  *  |__   __|

Press Ctrl-C to quit.
How It Works

After running import sevseg, you can call the sevseg.getSevSegStr() function to get a multiline string of the seven segment digits. However, the Countdown program needs to display a colon made out of asterisks in between the hours, minutes, and seconds. This requires splitting up the three lines of the multiline strings for these digits into three separate strings with the splitlines() method.

 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, artistic"""
 8.
 9. import sys, time
10. import sevseg  # Imports our sevseg.py program.
11.
12. # (!) Change this to any number of seconds:
13. secondsLeft = 30
14.
15. try:
16.     while True:  # Main program 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 = str(secondsLeft // 3600)
24.         # And 7265 % 3600 is 65, and 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 sevseg module:
30.         hDigits = sevseg.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 digits:
40.         print(hTopRow    + '     ' + mTopRow    + '     ' + sTopRow)
41.         print(hMiddleRow + '  *  ' + mMiddleRow + '  *  ' + sMiddleRow)
42.         print(hBottomRow + '  *  ' + mBottomRow + '  *  ' + sBottomRow)
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)  # Insert a one-second pause.
53.         secondsLeft -= 1
54. except KeyboardInterrupt:
55.     print('Countdown, by Al Sweigart [email protected]')
56.     sys.exit()  # When Ctrl-C is pressed, end the program.)

After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:

Exploring the Program

Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.

  1. What happens if you change secondsLeft = 30 on line 13 to secondsLeft = 30.5?
  2. What happens if you change the 2 on lines 30, 33, and 36 to 1?
  3. What happens if you change time.sleep(1) on line 52 to time.sleep(0.1)?
  4. What happens if you change secondsLeft -= 1 on line 53 to secondsLeft -= 2?
  5. What happens if you delete or comment out print('\n' * 60) on line 18?
  6. What error message do you get if you delete or comment out import sevseg on line 10?

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4