Creating a Simple 30-Line Python Program

Python is a powerful yet beginner-friendly programming language that allows us to write concise and effective programs. In this blog post, we’ll discuss the process of writing a simple 30-line Python program that performs a specific task.

The program we’ll create will be a basic “to-do list” application. It will allow the user to add, view, and complete tasks. Here’s the code for our 30-line Python to-do list program:

pythonclass TodoList:
def __init__(self):
self.tasks = []

def add_task(self, task):
self.tasks.append(task)

def view_tasks(self):
for index, task in enumerate(self.tasks, start=1):
print(f"{index}. {task}")

def complete_task(self, index):
if 1 <= index <= len(self.tasks):
self.tasks[index-1] = f"[COMPLETED] {self.tasks[index-1]}"
else:
print("Invalid task index.")


# Create a TodoList object
todo_list = TodoList()

# Add some tasks
todo_list.add_task("Learn Python")
todo_list.add_task("Finish project")
todo_list.add_task("Exercise")

# View tasks
print("\nCurrent tasks:")
todo_list.view_tasks()

# Complete a task
task_index = int(input("\nEnter the index of the task you want to complete (1-3): "))
todo_list.complete_task(task_index)

# View updated tasks
print("\nUpdated tasks:")
todo_list.view_tasks()

This program defines a TodoList class that has three methods: add_task(), view_tasks(), and complete_task(). The add_task() method allows us to add new tasks to the list, view_tasks() displays all the tasks, and complete_task() marks a task as completed by modifying its text.

We then create an instance of the TodoList class and use its methods to add some tasks, view them, complete a task, and view the updated list.

With this program, we demonstrate the use of classes and methods in Python, as well as basic input/output operations and list manipulation. It’s a simple yet functional program that can be extended further to include more features, such as task deletion or priority levels.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *