Skip to content

Instantly share code, notes, and snippets.

@shinysu
Created August 13, 2021 01:53
Show Gist options
  • Save shinysu/5ba3708c304678b7b1b2c5db6cc7c0c7 to your computer and use it in GitHub Desktop.
Save shinysu/5ba3708c304678b7b1b2c5db6cc7c0c7 to your computer and use it in GitHub Desktop.
Day8
'''
check if an element is present in the list
you can use 'in' operator to check if the element is present and 'not in' operator to check if the element is not present
'''
cart = ['ice cream', 'chocolates', 'bread', 'jam', 'butter']
item = input("Enter the element you want to search: ")
if item in cart:
print("yes, the element is present")
else:
print("No, the element is not present")
'''
iterate through the list and display elements in the list
'''
cart = ['ice cream', 'chocolates', 'bread', 'jam', 'butter']
for x in cart:
print(x)
def add_item():
item = input("Enter the task: ")
todolist.append(item)
print(todolist)
def delete_item():
item = input("Enter the element to be removed: ")
if item in todolist:
todolist.remove(item)
print(todolist)
def edit_task():
old_task = input("Enter the old task:")
new_task = input("Enter the new task: ")
indexno = todolist.index(old_task)
todolist[indexno] = new_task
print(todolist)
todolist =[]
while True:
choice = input("Enter your choice(add / delete / edit/ exit): ").lower()
if choice == 'add':
add_item()
elif choice == 'delete':
delete_item()
elif choice == 'edit':
edit_task()
elif choice == 'exit':
break
else:
print("Invalid choice")
In the todolist program, include two more options:
1. edit - To edit an item in the todolist
- Get the old item and the new item from the user.
- Remove the old item from the list and add the new item
2. display - Display all the elements in the list in sorted order
- Sort the list
- display the elements in the list using for loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment