Simple To-Do List
The code is a simple To-Do list application that allows users to add tasks, remove the last task, and quit the program.
The code is a simple To-Do list application that allows users to add tasks, remove the last task, and quit the program.
This script provides a basic command-line interface for managing a To-Do list. Users can add tasks to the list, remove the most recent task, or exit the application. The list of tasks is displayed each time an action is completed. If the list is empty, the program informs the user. The application operates in a loop, continuously offering the user the option to add or remove tasks until they choose to quit.
Check out the CodeImproving Task Removal:
pop()
method removes the last task from the list, which might not be the desired behavior if the user wants to remove a specific task. Instead, provide an option to remove a task by its index.elif choice == '2':
print('Removing task')
if todo_list:
index = int(input("Enter the task number to remove: ")) - 1
if 0 <= index < len(todo_list):
removed_task = todo_list.pop(index)
print(f"Task '{removed_task}' removed from the ToDo list")
else:
print("Invalid task number")
else:
print("ToDo List is empty")
Input Validation:
try:
choice = int(input('Which one do you choose 1, 2, 3?'))
if choice not in [1, 2, 3]:
print("Invalid choice, please choose 1, 2, or 3.")
continue
except ValueError:
print("Invalid input, please enter a number.")
continue
Task Listing Enhancement:
if not todo_list:
print('Your ToDo list is empty')
else:
print('Your ToDo List:')
for index, task in enumerate(todo_list, start=1):
print(f"{index}. {task}")
Modularize Code:
def display_tasks(todo_list):
if not todo_list:
print('Your ToDo list is empty')
else:
print('Your ToDo List:')
for index, task in enumerate(todo_list, start=1):
print(f"{index}. {task}")
def add_task(todo_list):
new_task = input("Enter the task: ")
todo_list.append(new_task)
print(f"Task '{new_task}' added to the ToDo list")
def remove_task(todo_list):
if todo_list:
display_tasks(todo_list)
try:
index = int(input("Enter the task number to remove: ")) - 1
if 0 <= index < len(todo_list):
removed_task = todo_list.pop(index)
print(f"Task '{removed_task}' removed from the ToDo list")
else:
print("Invalid task number")
except ValueError:
print("Invalid input, please enter a number.")
else:
print("ToDo List is empty")
todo_list = []
while True:
display_tasks(todo_list)
print('Options:')
print('1) Add Task')
print('2) Remove Task')
print('3) Quit')
try:
choice = int(input('Which one do you choose 1, 2, 3?'))
if choice == 1:
add_task(todo_list)
elif choice == 2:
remove_task(todo_list)
elif choice == 3:
print('Quitting')
break
else:
print("Invalid choice, please choose 1, 2, or 3.")
except ValueError:
print("Invalid input, please enter a number.")
User Feedback:
The code effectively manages a basic to-do list with options to add, remove, and quit. By adding input validation, improving task removal, and modularizing the code, you can enhance the usability and robustness of the application. This approach will make the code easier to understand, maintain, and extend with additional features in the future.