Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
more
Archives
Today
Total
관리 메뉴

금융을 따라 흐르는 블로그

creating a simple to-do list program using a list data structure: 본문

카테고리 없음

creating a simple to-do list program using a list data structure:

DeNarO 2023. 1. 19. 14:35

1. Here's an example of a Python script that creates a simple to-do list program using a list data structure:

todo_list = []

def add_todo(task):
    todo_list.append(task)
    print(f'Task "{task}" added to the list.')

def view_todo():
    if not todo_list:
        print("The list is empty.")
    else:
        for i, task in enumerate(todo_list):
            print(f'{i+1}. {task}')

def remove_todo(index):
    try:
        task = todo_list.pop(index-1)
        print(f'Task "{task}" removed from the list.')
    except IndexError:
        print("Invalid index. Please enter a valid index.")

while True:
    print("1. Add task")
    print("2. View list")
    print("3. Remove task")
    print("4. Exit")
    choice = input("What would you like to do? ")
    if choice == "1":
        task = input("Enter task: ")
        add_todo(task)
    elif choice == "2":
        view_todo()
    elif choice == "3":
        index = int(input("Enter index of task to remove: "))
        remove_todo(index)
    elif choice == "4":
        break
    else:
        print("Invalid choice. Please enter a valid choice.")

This script defines three functions: add_todo(), view_todo(), and remove_todo(), which allow the user to add, view, and remove tasks from the to-do list, respectively. The script also contains a while loop that presents the user with a menu of options and allows them to interact with the to-do list.


2. Here's an example of a Python script that creates a simple to-do list application using a list data structure to store the tasks:

todo_list = []

def add_task(task):
    todo_list.append(task)
    print("Task added: ", task)

def view_tasks():
    print("\nTo-do list:")
    for i, task in enumerate(todo_list):
        print(i+1, task)

def remove_task(index):
    del todo_list[index-1]
    print("Task removed.")

while True:
    print("\n1. Add task")
    print("2. View tasks")
    print("3. Remove task")
    print("4. Exit")
    choice = int(input("Enter your choice: "))
    if choice == 1:
        task = input("Enter task: ")
        add_task(task)
    elif choice == 2:
        view_tasks()
    elif choice == 3:
        index = int(input("Enter the index of the task to remove: "))
        remove_task(index)
    elif choice == 4:
        break
    else:
        print("Invalid choice.")

The above code creates a simple CLI interface that allows users to add, view, and remove tasks from the to-do list. The to-do list is stored in a Python list and tasks are added to the list using the append() method. The script also allows the user to view the current tasks by printing the list, and remove a task by using the del function, passing the index of the task to remove.

 

Please note that the above code is a simple example, and in real-world applications, you may want to consider using a database to store the to-do list tasks and other more advanced features.


3. Here's an example of a Python script that creates a basic to-do list program using a list and a while loop:

todo_list = []

while True:
    print("Enter 'add' to add an item to the list.")
    print("Enter 'remove' to remove an item from the list.")
    print("Enter 'view' to view the list.")
    print("Enter 'quit' to exit the program.")
    user_input = input("What would you like to do? ")

    if user_input.lower() == 'add':
        item = input("What would you like to add? ")
        todo_list.append(item)
        print("Item added.")
    elif user_input.lower() == 'remove':
        item = input("What would you like to remove? ")
        todo_list.remove(item)
        print("Item removed.")
    elif user_input.lower() == 'view':
        print("To-Do List:")
        for item in todo_list:
            print(item)
    elif user_input.lower() == 'quit':
        break
    else:
        print("Invalid input.")

This script uses a while loop to keep the program running until the user enters "quit". The user can add items to the list by entering "add", remove items from the list by entering "remove", view the list by entering "view", and exit the program by entering "quit".

 

This is a very basic example and it can be enhanced with more features such as persistency (saving the list for future use) or adding a priority level for the task, or even a deadline.


4. Here's an example of a Python script that uses a list to create a basic to-do list program:

tasks = []

def add_task(task):
    tasks.append(task)
    print("Task added: ", task)

def view_tasks():
    if not tasks:
        print("No tasks to display.")
    else:
        for i, task in enumerate(tasks):
            print(f"{i+1}. {task}")

def complete_task(index):
    try:
        task = tasks.pop(index - 1)
        print("Task completed: ", task)
    except:
        print("Invalid task index.")

while True:
    action = input("What would you like to do? (add/view/complete) ")
    if action == "add":
        task = input("Enter task: ")
        add_task(task)
    elif action == "view":
        view_tasks()
    elif action == "complete":
        index = int(input("Enter task index: "))
        complete_task(index)
    elif action == "exit":
        break
    else:
        print("Invalid action.")

This script allows a user to add tasks to a list, view all tasks, and complete tasks by their index. The script will continue to run until the user types "exit".

This code is just an example, you can add more features and improve the functionality according to your needs.