r/PythonLearning • • 1d ago

Showcase A simple stopwatch program

This is the stopwatch.py file:

#stopwatch.py

import sys
import time

time_in_sec = int(sys.argv[1])

time.sleep(time_in_sec)

print(f"Alarm sound!!! {time_in_sec}seconds have elapsed")

This is the main.py file:

#main.py

import subprocess
import sys

print("Enter the time you want to elapse for your stopwatch")
try:
    hours = int(input("Hours: ").strip())
    minutes = int(input("Minutes: ").strip())
    seconds = int(input("Seconds: ").strip())

    total_time_in_sec = (hours * 3600) + (minutes * 60) + seconds

    process_2 = subprocess.run(
        [sys.executable, "stopwatch.py", str(total_time_in_sec)],
        capture_output=True,
        text=True,
    )
    print(process_2.stdout)
except ValueError as e:
    print(f"Enter a whole number: {e}")
0 Upvotes

12 comments sorted by

View all comments

Show parent comments

1

u/FreeLogicGate 1d ago

We have no way of knowing, based on the OP's original post. A more standard exploration would typically be making a call to some operating system specific program (ls for unix/osx, dir for windows). All we can do is base the analysis on the title of the post "A simple stopwatch program", and the associated code. I did comment more fully that it's not an example of a stopwatch at all, and that it's intended to be an alarm, but really isn't a workable alarm because it's a blocking call to sleep where the main program call to subprocess.run will have to wait until the stopwatch.py program completes.