diff --git a/calculator/main.py b/calculator/main.py index 43bf23b..faf1baf 100644 --- a/calculator/main.py +++ b/calculator/main.py @@ -2,20 +2,22 @@ # Performs basic arithmetic operations based on user input def add(a, b): - # TODO: Return sum of a and b + return a + b pass def subtract(a, b): - # TODO: Return difference of a and b + return a - b pass def multiply(a, b): - # TODO: Return product of a and b + return a * b pass def divide(a, b): - # TODO: Return quotient of a and b - # Handle division by zero + if b == 0: + return "Error: division by 0" + else: + return a/b pass def main(): @@ -27,7 +29,18 @@ def main(): operator = input("Enter operator: ") num2 = float(input("Enter second number: ")) - # Match the operator with the correct function + if operator == "+": + answer = add(num1, num2) + elif operator == "-": + answer = subtract(num1, num2) + elif operator == "*": + answer = multiply(num1, num2) + elif operator == "/": + answer = divide(num1, num2) + else: + answer = "Error: invalid operator" + + print("Answer: ", answer) except ValueError: print("Invalid number entered.") diff --git a/convert-distance/main.py b/convert-distance/main.py index f6bcfe8..3bbd95d 100644 --- a/convert-distance/main.py +++ b/convert-distance/main.py @@ -2,15 +2,18 @@ # Convert between kilometers, meters, and centimeters def km_to_m(km): - # TODO: Convert kilometers to meters + m = km * 1000 + print("km = ", m, "m") pass def m_to_cm(m): - # TODO: Convert meters to centimeters + cm = m * 100 + print("m = ", cm, "cm") pass def cm_to_km(cm): - # TODO: Convert centimeters to kilometers + km = cm / 100000 + print("cm = ", km, "km") pass def main(): @@ -24,13 +27,13 @@ def main(): if choice == "1": km = float(input("Enter distance in kilometers: ")) - # Call conversion function + km_to_m(km) elif choice == "2": m = float(input("Enter distance in meters: ")) - # Call conversion function + m_to_cm(m) elif choice == "3": cm = float(input("Enter distance in centimeters: ")) - # Call conversion function + cm_to_km(cm) else: print("Invalid choice!") diff --git a/digital-clock/main.py b/digital-clock/main.py index 986773e..bb4e62d 100644 --- a/digital-clock/main.py +++ b/digital-clock/main.py @@ -5,16 +5,17 @@ import time def get_current_time(): - # TODO: Return the current system time in formatted string + return datetime.now().strftime("%H:%M:%S") pass def main(): - print("Digital Clock - Press Ctrl+C to stop\n") + print("Digital Clock - Press Ctrl+C to stop") try: while True: - # Get current time and display it - # Use time.sleep(1) to update every second + current_time = get_current_time() + print("\r" + current_time, end="") + time.sleep(1) pass except KeyboardInterrupt: print("\nClock stopped.") diff --git a/pick-color/main.py b/pick-color/main.py index 4286499..f8d9dd9 100644 --- a/pick-color/main.py +++ b/pick-color/main.py @@ -4,16 +4,20 @@ import random def get_random_color(): - # TODO: Return a random color from the list colors = ["Red", "Green", "Blue", "Yellow", "Purple", "Orange"] + return random.choice(colors) pass def main(): print("Random Color Picker") - while True: - input("Press Enter to pick a color (or Ctrl+C to quit)...") - # Call the random color function and display the result + try: + while True: + input("Press Enter to pick a color (or Ctrl+C to quit)...") + color = get_random_color() + print("Your color is: ", color) + except KeyboardInterrupt: + print("Stopped.") if __name__ == "__main__": main() diff --git a/to-do/main.py b/to-do/main.py index a1fcc80..7ab18cc 100644 --- a/to-do/main.py +++ b/to-do/main.py @@ -9,15 +9,36 @@ def show_menu(): print("4. Exit") def add_task(tasks): - # TODO: Ask user for a task and append to the list + task = input("Enter a new task: ") + tasks.append(task) + print("Task added.") pass def remove_task(tasks): - # TODO: Show task list and allow user to remove by index + if len(tasks) == 0: + print("No tasks to remove.") + pass + view_tasks(tasks) + try: + index = int(input("Enter the task number to remove: ")) + if 1 <= index <= len(tasks): + tasks.pop(index - 1) + print("Task removed.") + else: + print("Invalid task number.") + except ValueError: + print("Error: please enter a valid number") pass def view_tasks(tasks): - # TODO: Display current list of tasks + if len(tasks) == 0: + print("No tasks added yet.") + else: + count = 1 + print(tasks) + for task in tasks: + print(count, ". ", task) + count += 1 pass def main():