External Exam Download Resources Web Applications Games Recycle Bin

Track Companion

timing1.py

from datetime import datetime

input("press enter to start")
start = datetime.now()
input("press enter to stop")
end = datetime.now()
print("time taken: " + str(end - start))


timing2.py

from datetime import datetime

input("press enter to start")
start = datetime.now()
print("started at: " + str(start.strftime("%H:%M:%S.%f")[:-4]))

input("press enter to stop")
end = datetime.now()
print("stopped at: " + str(end.strftime("%H:%M:%S.%f")[:-4]))

print("time taken: " + str(end - start)[:-4])


timing3.py

from datetime import datetime

while True:
    try:
        choice = int(input("1 = Start. 2 = Clear. Q = Quit: "))
        if choice == 1:
            start = datetime.now()
            print("Start: " + str(start.strftime("%H:%M:%S.%f")[:-4]))

            input("\n...Press Enter to Stop...")
            end = datetime.now()
            print("Stopped: " + str(end.strftime("%H:%M:%S.%f")[:-4]))

            print("Time Elapsed: " + str(end - start)[:-4])
        
        else:
            print("\n" * 100)
    except:
        break


timing4.py

import PySimpleGUI as PSG
from datetime import datetime

display = PSG.Text("0:00")

rows = [
            [display]
        ]

form = PSG.FlexForm("Stopwatch")
form.Layout(rows)
started = datetime.now()

while True:
    button, value = form.ReadNonBlocking()
    current = datetime.now()
    elapsed = current - started
    display.Update(str(elapsed.seconds))

timing5.py

import PySimpleGUI as PSG
from datetime import datetime

display = PSG.Text("0:00")

rows = [
            [display],
            [PSG.ReadFormButton("Start")],
            [PSG.ReadFormButton("Stop")]
        ]

form = PSG.FlexForm("Stopwatch")
form.Layout(rows)

running = False
started = datetime.now()

while True:
    button, value = form.ReadNonBlocking()
    
    if running:
        current = datetime.now()
        elapsed = current - started
        display.Update(str(elapsed.seconds) +
                        ":" +
                        str(elapsed.microseconds)[:2])
        

    if button == "Start":
        started = datetime.now()
        running = True

    if button == "Stop":
        running = False

For this project you are going to have to integrate some timing modules in Python. The examples present a mix of GUI and non-GUI options. It is important to note that these examples are not completed solutions to the project task(s) required. You will have to utilise your skills in Python and integrate some of the timing modules demonstrated here to produce your best result.