Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 0 additions & 254 deletions projects/final-project/solution/danieleFiocca/index.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import time
import threading


def set_timer():
Copy link

@effedib effedib Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this func contains a bug, if you enter a number <= 0 it starts an infinite loop (and this is the reason why we have to avoid infinite loops) without exit caused by absence of another input inside the loop.
This changed version would work correctly:
while (timer := int(input("Enter a time in seconds: "))) <= 0:
print("Value of timer must be greater than 0.")
return timer

"""Setting the timer in seconds"""
timer = int(input("Enter a time in seconds: "))
while True:
if timer > 0:
return timer
else:
print("Value of timer must be greater than 0.")


def get_format_input():
"""Setting the format for display countdown"""
print("\nChoose time format:")
print("1. Complete (DD:HH:MM:SS)")
print("2. Hour:Minute:Seconds")
print("3. Minute:Seconds")
print("4. Seconds")

while True:
format_select = int(input("\nEnter a time format: "))
if format_select in [1, 2, 3, 4]:
return format_select
else:
print("Selection must be 1 to 4.")


def format_display_timer(seconds, format_selected):
"""Formatting the display countdown"""
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
if format_selected == 1:
return '{:02d}:{:02d}:{:02d}:{:02d}'.format(days, hours, minutes, seconds)
if format_selected == 2:
total_hours = days * 24 + hours
return '{:02d}:{:02d}:{:02d}'.format(total_hours, minutes, seconds)
if format_selected == 3:
total_minutes = (days * 24 * 60) + (hours * 60) + minutes
return '{:02d}:{:02d}'.format(total_minutes, seconds)
if format_selected == 4:
return '{:04d}'.format(seconds)


def countdown_logic(initial_seconds, format_selected, pause_event, stop_event, reset_event):
"""Countdown logic"""
seconds = initial_seconds
while seconds >= 0:
if stop_event.is_set():
return

if reset_event.is_set():
seconds = initial_seconds
reset_event.clear()
print(f"\nReset to: {format_display_timer(seconds, format_selected)}")

if not pause_event.is_set():
print(f"\r{format_display_timer(seconds, format_selected)} ", end="", flush=True)
time.sleep(1)
seconds -= 1
else:
time.sleep(0.1) # While paused
if not stop_event.is_set():
print("\nTimer finished.")


def handle_commands(pause, stop, reset):
"""Logic for threading commands"""
print("Command available: start, pause, stop, reset")

while True:
command = input("\n>>>").strip().lower()
if command == "start":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to use match case statement

pause.clear()
print("Timer started")
elif command == "pause":
pause.set()
print("Timer paused")
elif command == "reset":
reset.set()
print("Timer reset")
elif command == "stop":
stop.set()
print("Timer stopped")
break
else:
print("Command not available")


def start_timer():
"""Function to start the timer """
seconds = set_timer()
format_selected = get_format_input()

pause = threading.Event()
stop = threading.Event()
reset = threading.Event()

timer_thread = threading.Thread(target=countdown_logic, args=(seconds, format_selected, pause, stop, reset))
timer_thread.start()

handle_commands(pause, stop, reset)

timer_thread.join()
print("\nTimer finished.")



Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from datetime import datetime


def current_date_time():
return datetime.now().strftime('%d/%m/%Y %H:%M:%S')


def day_of_the_week():
return datetime.now().strftime('%A')


def day_of_the_year():
return datetime.now().strftime('%j')


def week_number():
return datetime.now().strftime('%W')


def show_date_time():
print("--- DATA AND TIME INFORMATION ---")
print(f"\nDate and time: {current_date_time()}")
print(f"Day of the week: {day_of_the_week()}")
print(f"Day of the year: {day_of_the_year()}")
print(f"Week number: {week_number()}\n")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from main import main

if __name__ == '__main__':
main()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the entrypoint would be main.py not index.py, better to switch them

Loading