r/PythonLearning • • 28d 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

2

u/FreeLogicGate 28d ago

Makes you question whether or not the op knows how to create a function.

4

u/tiredITguy42 28d ago

OP is a beginner, all beginners do stupid stuff, but he must get this idea from somewhere and that scares me.

1

u/osamas_den 28d ago

Yes I'm a beginner, don't be scared i did not use AI, I just stare at a problem long enough until somethin clicks. I agree using a function might have been easier but still wanted to get standard output from the other instance of the python process.

1

u/tiredITguy42 28d ago

You run other python process with multiprocessing library. This is rather for other apps. You may try with ping or with some bat file. There is not much cases you would need sunrpocess for as beginner. You do not need curl as you can use python to run apic all. File linraries handle files. This really just for some exe or bat situations when you have some third party app you need to run from oython.