Skip to content

Commit

Permalink
Create to_do_list.py
Browse files Browse the repository at this point in the history
  • Loading branch information
otahina authored Jul 17, 2023
1 parent 55ec8bc commit bde7116
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions projects/ToDoList/to_do_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
def add_task(task):
todo_list.append(task)
print("Task added!")

def remove_task(task_num):
if 0 <= task_num < len(todo_list):
del todo_list[task_num]
print("Task removed!")
else:
print("Invalid task number!")

def display_tasks():
if not todo_list: # If list is empty
print("No tasks to display.")
else:
for index, task in enumerate(todo_list, start=1):
print(f"{index}. {task}")

def get_choice():
while True:
try:
choice = int(input("Type a number: 1. Adding a task, 2. Removing a task, 3. Displaying tasks, 4. Quit: "))
if 1 <= choice <= 4:
return choice
else:
print("Invalid choice. Try again.")
except ValueError:
print("Please enter a number between 1 and 4.")

if __name__ == "__main__":

todo_list = []

print("Welcome to ToDo List!")

while True:
user_choice = get_choice()

#Adding a task
if user_choice == 1:
new_task = input("Type a new task: ")
add_task(new_task)

#Removing a task
elif user_choice == 2:
if not todo_list: # If list is empty
print("No tasks to remove.")
else:
task_num = int(input("Enter the task number to delete: ")) - 1
remove_task(task_num)

#Displaying tasks
elif user_choice == 3:
display_tasks()

#Quit
elif user_choice == 4:
print("Goodbye!")
break

0 comments on commit bde7116

Please sign in to comment.