Skip to content

Instantly share code, notes, and snippets.

@Venkat-Swaraj
Created May 7, 2022 10:00
Show Gist options
  • Save Venkat-Swaraj/f5c7acc6248e31cb1ecda833627e0bfd to your computer and use it in GitHub Desktop.
Save Venkat-Swaraj/f5c7acc6248e31cb1ecda833627e0bfd to your computer and use it in GitHub Desktop.
Timer problem in Python
program in the file observe.py that prints out the calls for a spaceship that is about to launch. Countdown from 10 to 1 and then output Liftoff!
Your uses a for loop.
Here's a sample run of the program:
$ python3 Observe.py
10
9
8
7
6
5
4
3
2
1
Liftoff!
Run it and check the output
Now if you got an idea of how it runs then designe a digital clock using
nested loops
the format should be hh:mm:ss
for every 0-59 total 60 seconds minites will be updated to 1 and for every 60 minutes that is 0-59 minutes hours will be updated by 1
use the previous Observe.py program and design your own logic for this
"""
Prints out a spaceship launch sequence.
"""
import time
def main():
for i in range(10,0,-1):
if(i < 10):
print("0",end="")
print(i, end="", flush = True)
print("\b\b",end="")
time.sleep(1)
print("Liftoff!")
if __name__ == '__main__':
main()
# solution code
import time
def main():
sec = 0
min = 0
hr = 0
while sec < 60:
print("{:02d}:{:02d}:{:02d}".format(hr,min,sec), end="", flush = True)
sec += 1
if sec == 60:
sec = 0
min += 1
if min == 60:
min = 0
hr += 1
if hr == 24:
break
print("\b"*8,end="")
print("Liftoff!")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment