Question

In: Computer Science

I'm working on a to-do list program in Python 2. I'm trying to delete an item...

I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue: ")) after each if statement?

def menu():

   print "[1] Add an item to the list\n[2] Delete an item from the list\n[3] Print the list\n[4] Print the list in reverse\n[5] Quit the program"

menu()

menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue: "))

todo_list = []

i = 0

count = 0

while menu_option != 5:

   

   if menu_option == 1:

      addedItem = str(input("Please add an item to the To-Do List. Type 'done' when you have entered all items."))

      if addedItem == 'done':

         break

      todo_list.append(addedItem)

      i += 1

      count += 1

   else: i += 1

   

  

   if menu_option == 2:

      deletedItem = str(input("Please enter the number of the item you would like to delete."))

      todo_list.remove(deletedItem)

    

      print "Item " + str(deletedItem) + " has been deleted"

Solutions

Expert Solution

def menu():
    print("[1] Add an item to the list\n[2] Delete an item from the list\n[3] Print the list\n[4] Print the list in reverse\n[5] Quit the program")


menu()
menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue: "))
todo_list = []
i = 0
count = 0
while menu_option != 5:
    if menu_option == 1:
        while(True):
            addedItem = str(input("Please add an item to the To-Do List. Type 'done' when you have entered all items."))
            if addedItem == 'done':
                break
            todo_list.append(addedItem)
            i += 1
            count += 1
    elif menu_option == 2:
        if(len(todo_list) == 0):
            print("No items in todo_list. Please add items and then use option 2 to delete item.")
        else:
            deletedItem = str(input("Please enter the number of the item you would like to delete."))
            todo_list.remove(deletedItem)
            print("Item " + str(deletedItem) + " has been deleted")
    menu()
    menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue: "))


Related Solutions

I'm working on a scatter-plot program in Python using Pandas, Matplotlib, Numpy, etc. I'm pulling data...
I'm working on a scatter-plot program in Python using Pandas, Matplotlib, Numpy, etc. I'm pulling data from a CSV file, which has no names, just numbers. All I did was to read a .csv file. How do I pull data from three columns which contains about 1500 rows with just numbers and make a scatter plot with two in the x-axis and the third in the y-axis?
PYTHON--- regarding a hash table, why would it be bad to delete an item to successive...
PYTHON--- regarding a hash table, why would it be bad to delete an item to successive searches or insertions that uses open addressing
I'm trying to write a feet to meters and centimeters program and I'm looking for a...
I'm trying to write a feet to meters and centimeters program and I'm looking for a little help so I can see how close I got. It should do the following: Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard...
You are asked to delete the last occurrence of an item from a linked list. So,...
You are asked to delete the last occurrence of an item from a linked list. So, for instance: Input: 2 -> 3 -> 2 -> 4 Delete last occurrence of 2, result: 2 -> 3 -> 4 Implement the following method to do the deletion. You may NOT use or implement helper methods - all your code must be implemented inside the given method. You may NOT use recursion. public class Node { public int data; public Node next; }...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a dictionary, WITHOUT using pandas. I'm able to import pickle and can open my file...but I'm not sure how to create the dictionary.
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
Hello! I'm trying to work on a python lab in my class, and we basically have...
Hello! I'm trying to work on a python lab in my class, and we basically have to make this method play_interactive Now for the fun bit. The function play_interactive will take just one argument --- the length of patterns to use as keys in the dictionary --- and will start an interactive game session, reading either '1' or '2' from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session...
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
1- Function 1: to delete a node in the head of the list. 2- Function 2:...
1- Function 1: to delete a node in the head of the list. 2- Function 2: to delete a node in the end of the list. 3- Function 3: to delete a node in the middle of the list. Ask the user the value of the node to delete. 4- Test the three functions in the main() and display the new list after each delete. #include <iostream> using namespace std; struct node { int num; node * nextptr; }*head,*curnode; node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT