0% found this document useful (0 votes)
2 views

python

This document contains a simple Python program for managing a To-Do List. Users can view tasks, add new tasks, mark tasks as done, and save the list to a file. The program includes functions for each of these features and handles file operations for loading and saving tasks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python

This document contains a simple Python program for managing a To-Do List. Users can view tasks, add new tasks, mark tasks as done, and save the list to a file. The program includes functions for each of these features and handles file operations for loading and saving tasks.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

python

# Simple To-Do List in Python

def show_menu():

print("1. View To-Do List")

print("2. Add Task")

print("3. Mark Task as Done")

print("4. Save and Exit")

def view_tasks(tasks):

if not tasks:

print("No tasks in the To-Do List.")

else:

for index, task in enumerate(tasks, start=1):

print(f"{index}. {task}")

def add_task(tasks, new_task):

tasks.append(new_task)

print(f"Task '{new_task}' added successfully.")

def mark_as_done(tasks, task_index):

if 1 <= task_index <= len(tasks):

done_task = tasks.pop(task_index - 1)

print(f"Task '{done_task}' marked as done.")

else:

print("Invalid task index.")

def save_to_file(tasks, filename="todo.txt"):

with open(filename, "w") as file:

for task in tasks:

file.write(task + "\n")
def load_from_file(filename="todo.txt"):

try:

with open(filename, "r") as file:

tasks = [line.strip() for line in file.readlines()]

return tasks

except FileNotFoundError:

return []

def main():

tasks = load_from_file()

while True:

show_menu()

choice = input("Enter your choice (1-4): ")

if choice == "1":

view_tasks(tasks)

elif choice == "2":

new_task = input("Enter the new task: ")

add_task(tasks, new_task)

elif choice == "3":

task_index = int(input("Enter the task index to mark as done: "))

mark_as_done(tasks, task_index)

elif choice == "4":

save_to_file(tasks)

print("To-Do List saved. Exiting...")

break

else:

print("Invalid choice. Please enter a number between 1 and 4.")


if __name__ == "__main__":

main()

You might also like