How to make TODO app using python

I was confused about how to make a TODO task managing app using python. I can make the rest but I want to assign importance to a task and i want to print the tasks according to their impotance in descending order.

Repl link:

def printt():
  for row in list:
    for item in row:
      print(f"{item}")
print("""TODO List Managment System

1: Add
2: View
3: Remove
4: Edit""")
w=input()
if w=='1':
  add=input("What do you want to add: ")
  due=input("When it is due: ")
  impo=input("Importance(1-4): ")
  row=[add,due,impo]
  list.append(row)
  printt()
elif w=='2':
  rem=input("What do you want to remove:")
  for row in list:
    if rem in row:
      list.remove(rem)
elif w=='3':
  printt()
elif w=='4':
  print("\033[31m","Your TODO list is:","\033[0m")
  printt()
  print("\033[31m","Which task do you want to edit:","\033[0m")
  edit=input("What do you want to edit")
  for row in list:
    if edit in row:
      list[row]=input("What do you want to replace it with?")
    else:
      print("That task does not exists.")

Welcome to the community @MushtaqAhmed4

Let’s go step by step.

First: do not use list to create a list, because in Python list is a built-in data type. So we should rename list to task_list.

Second, you ask the user for input once and then stop. Usually, you want the program to keep running until the user decides to stop. So in this scenario is best to wrap the entire thing in a while True: loop until the break.

Third: you mixed up “remove” and “edit”.

Fourth: you should add an error message for invalid choice, if the user enters anything other than 1-5, it’s a good pratice to print an error and ask for input again.

Here is the code with the adjustments:

def print_tasks():
    task_list.sort(key=lambda x: -int(x[2]))
    for row in task_list:
        print(f"{row}")

task_list = []

while True:
    print("""TODO List Management System
    1: Add
    2: View
    3: Remove
    4: Edit
    5: Exit""")
    w = input()
    if w == '1':
        add = input("What do you want to add: ")
        due = input("When it is due: ")
        impo = input("Importance(1-4): ")
        row = [add, due, impo]
        task_list.append(row)
    elif w == '2':
        print_tasks()
    elif w == '3':
        rem = input("What do you want to remove: ")
        task_list = [row for row in task_list if row[0] != rem]
    elif w == '4':
        old_task = input("Which task do you want to edit? ")
        for i, row in enumerate(task_list):
            if row[0] == old_task:
                new_task = input("What do you want to change it to: ")
                new_due = input("When it is due: ")
                new_impo = input("Importance(1-4): ")
                task_list[i] = [new_task, new_due, new_impo]
    elif w == '5':
        print("Exiting...")
        break
    else:
        print("Invalid choice, please choose a number between 1-5.")

3 Likes

Just helping Qwerty here,

‘scenario’

‘thing’

1 Like

Thanks! It’s easy to get confused when English is not your first language hahaha

1 Like

It’s not your first language?

1 Like

Yes, I am Brazilian. And even though I’ve never had English lessons, I try to speak the best I can, so sometimes I may get confused while writing something or accidentally type something in Portuguese out of habit.

2 Likes

Thanks for your help, but I can solve those problems that you mentioned. I need a method that allows me to set the importance of the tasks and show them in their order. Any ideas on that? I tried making four lists for each importance but that is not the most efficient way (I believe), there has to be another way. Thanks a lot if anyone could help.

Oh, you mean data validation from the importance. You can define a new function to make the validation.

def valid_importance(impo):
    if not impo.isdigit() or int(impo) < 1 or int(impo) > 4:
        return False
    return True

def print_tasks():
    task_list.sort(key=lambda x: -int(x[2]))
    for row in task_list:
        print(f"{row}")

task_list = []

while True:
    print("""TODO List Management System
    1: Add
    2: View
    3: Remove
    4: Edit
    5: Exit""")
    w = input()
    if w == '1':
        add = input("What do you want to add: ")
        due = input("When it is due: ")
        impo = input("Importance(1-4): ")
        while not valid_importance(impo):
            print("Invalid importance. Please enter a number between 1 and 4.")
            impo = input("Importance(1-4): ")
        row = [add, due, impo]
        task_list.append(row)
    elif w == '2':
        print_tasks()
    elif w == '3':
        rem = input("What do you want to remove: ")
        task_list = [row for row in task_list if row[0] != rem]
    elif w == '4':
        old_task = input("Which task do you want to edit? ")
        for i, row in enumerate(task_list):
            if row[0] == old_task:
                new_task = input("What do you want to change it to: ")
                new_due = input("When it is due: ")
                new_impo = input("Importance(1-4): ")
                while not valid_importance(new_impo):
                    print("Invalid importance. Please enter a number between 1 and 4.")
                    new_impo = input("Importance(1-4): ")
                task_list[i] = [new_task, new_due, new_impo]
    elif w == '5':
        print("Exiting...")
        break
    else:
        print("Invalid choice, please choose a number between 1-5.")

The task_list.sort(key=lambda x: -int(x[2])) sorts the task_list in descending order of importance. The tasks with the highest importance are printed first.

Explaining further:
The -int(x[2]) is used to sort the tasks in descending order of importance (4 is highest and 1 is lowest). Here, x represents an individual task (which is a list of three elements: task name, due date, and importance), and x[2] is the importance of the task. We convert it to an integer and negate it to sort in descending order, because the sort() function alone, sorts in ascending order by default.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.