Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 19 additions & 6 deletions calculator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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.")
Expand Down
15 changes: 9 additions & 6 deletions convert-distance/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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!")

Expand Down
9 changes: 5 additions & 4 deletions digital-clock/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
12 changes: 8 additions & 4 deletions pick-color/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
27 changes: 24 additions & 3 deletions to-do/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down