Here's a basic Python program that implements a simple "to-do list" application:
This program allows the user to add tasks to a list, view the current tasks in the list, and mark tasks as completed.
Here's a breakdown of how it works:
The program starts by initializing an empty list named
todo_list
that will store all the tasks.The program defines three functions:
add_task(task)
which takes one parameter,task
, which is a string. This function appends thetask
totodo_list
and prints "Task added: task_name"view_tasks()
which takes no parameter. This function checks if thetodo_list
is empty or not. If it's empty, it prints "No tasks to display.". If not empty it will enumerate the todo_list and prints all the tasks in the list.complete_task(task_number)
which takes one parameter,task_number
, which is an integer. This function checks if the task_number is valid, if it's valid it removes the task from the todo_list and prints "Task completed: task_name" if not it raises an exception and prints "Invalid task number."
The program then enters an infinite loop using
while
keyword. Inside the loop, the program prints a menu of options the user can choose from: add task, view tasks, complete task, or exit.Next, the program prompts the user to enter their choice of operation using the
input()
function. The user's input is stored in the variablechoice
.Then the program uses
if-elif-else
statement to check the value ofchoice
variable.
- If choice is 1, the program prompts the user to enter the task to be added, calls the
add_task()
function with the entered task as an argument. - If choice is 2, the program calls the
view_tasks()
function. - If choice is 3, the program prompts the user to enter the task number to be completed, calls the
complete_task()
function with the entered task number as an argument. - If choice is 4, the program exits the loop and the program ends.
- If the user enters an invalid choice, the program will print "Invalid Input."
After this explanation this is how it works overall. The program uses basic data structure 'list' and control flow statements like if-elif-else
, while
loop, function, input output operation to create a basic to-do list application. It's simple and easy to understand, you can add more functionality and improve the user interface to make it more user friendly.
Please let me know if you have any more questions or if there's anything else I can do for you.
No comments:
Post a Comment