11/14/2022 0 Comments Loopback todolist![]() ![]() View_tasks() # calls function to view all tasks LOOPBACK TODOLIST CODEI'll explain them a bit more in detail in the code with comments and also after this code block: You shall all notice that I'll add some other small pieces of code those aren't really neccessary, but make the program a bit cooler. We'll use append() to add it to the list.įinally, we'll have a break, which will cause the program to exit the while True: loop and carry on with the rest of the program. The second most important thing is to add this new task to the not_completed_tasks which, if you recall, is the list holding all the not completed tasks. This is because when you initially create a task, the task is by default not completed yet, that's why we automatically put the value as an empty square. The most important parts will be actually creating the new task meaning that we add it inside the dict as a key, and add the empty square as value. In this else statement, we'll have quite a few things. Now, let's add an else statement so that when the user enters a valid name for the task (a name not the same to any other existing tasks), they can continue with the rest of the program. Then, we use pass to kind of "pass" and carry on with the loop. If this expression evaluates to True, then the code inside that if statement shall execute the program shall tell the user (with a print() statement) that they already have a task with the same name. ![]() Then, using an if statement, we check if the name of that new task, new_task_name is already in the dict holding all the tasks, todo_name_done. So first, we have the input(), and we store what the user says in the variable new_task_name. Print("Sorry, you already used this task name!") New_task_name = input("New task? Let's add a name\n¬") ![]() # asks user what the name of the new task should be. While True: # loop to continue as long as user enters invalid name for task Wooh, you've done it! We can now view tasks. Finally, it outputs the key (the name of the task) i. Then, it outputs todo_name_done which gets the value of the key (the empty/full squares). I'll be using ANSI escape codes but I'll cover how exactly to implement the colors somewhere, later.īack on that print using concatenation again, it first outputs the n 1 (converted into str), because remember, enumerate starts at 0, and we don't wwant the first task to be labeled as 0. In the elif statement, you could color the print red to indicate that the following task is not completed. The only thing is that you could insert colors inside the print statement so for example, in the if statement, you could color that print green to indicate that the following task is completed. It's the same thing with the elif, except that it checks if the task is not completed (the empty square).īoth branches of the conditionals output the same thing: print(str(n 1) ") " todo_name_done " " i). ![]() And i is task name that the for loop is currently on. So in this case, to get the value (the square) of a task name (the key), we do todo_name_done. And because the for loop loops through the dict's keys, to get the value of a key you do name_of_dict. The task name is the key, and the empty/full square is the value. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |