'''
List Maintainer

Summary
A program that allows the user to add, delete and organize the items in a list. 
'''

import time

def add_item(PandR_list):
    print("\n\n[Add item]")
    workingItem = input("Enter item (all lowercase) to add: ")
    if workingItem in PandR_list:
        print("Error: Item already in list.")
        time.sleep(2)
    else:
        PandR_list.append(workingItem)
        print("Item was added to list.")
        time.sleep(2)
        
def remove_item(PandR_list):
    print("\n\n[Remove item]")
    workingItem = input("Enter item (all lowercase) to be removed: ")
    if workingItem not in PandR_list:
        print("Error: Item not in list.")
        time.sleep(2)
    else:
        PandR_list.remove(workingItem)
        print("Item was removed from list.")
        time.sleep(2)
        
def move_item_up(PandR_list):
    print("\n\n[Move item up]")
    workingItem = input("Enter item (all lowercase) to move up: ")
    if workingItem not in PandR_list:
        print("Error: Item not in list.")
    else:
        for existingItem in range(len(PandR_list)):
            if workingItem == PandR_list[existingItem]:
                itemIndex = existingItem
                if itemIndex == 0:
                    print("Error: Item is already at top of list.")
                    time.sleep(2)
                else:
                    PandR_list[itemIndex - 1], PandR_list[itemIndex] = PandR_list[itemIndex], PandR_list[itemIndex - 1]
                    print("Item was moved up.")
                    time.sleep(2)
                                      
def move_item_down(PandR_list):
    print("\n\n[Move item down]")
    workingItem = input("Enter item (all lowercase) to move down: ")
    if workingItem not in PandR_list:
        print("Error: Item not in list.")
        time.sleep(2)
    else:
        for existingItem in range(len(PandR_list)):
            if workingItem == PandR_list[existingItem]:
                itemIndex = existingItem
                if itemIndex == len(PandR_list)-1:
                    print("Error: Item is already at bottom of list.")
                    time.sleep(2)
                else:
                    PandR_list[itemIndex + 1], PandR_list[itemIndex] = PandR_list[itemIndex], PandR_list[itemIndex + 1]
                    print("Item was moved down.")
                    time.sleep(2)
                    break
                    
def move_item_top(PandR_list):
    print("\n\n[Move item to top of list]")
    workingItem = input("Enter item (all lowercase) to move to top of list: ")
    if workingItem not in PandR_list:
        print("Error: Item not in list.")
        time.sleep(2)
    else:
        for existingItem in range(len(PandR_list)):
            if workingItem == PandR_list[existingItem]:
                itemIndex = existingItem
                if itemIndex == 0:
                    print("Error: Item is already at top of list.")    
                else:
                    PandR_list.pop(itemIndex)
                    PandR_list.insert(0, workingItem)
                    print("Item was moved to top of list.")
                    time.sleep(2)
                    
def move_item_bottom(PandR_list):
    print("\n\n[Move item to bottom of list]")
    workingItem = input("Enter item (all lowercase) to move to bottom of list: ")
    if workingItem not in PandR_list:
        print("Error: Item not in list.")
        time.sleep(2)
    else:
        for existingItem in range(len(PandR_list)):
            if workingItem == PandR_list[existingItem]:
                itemIndex = existingItem
                if itemIndex == len(PandR_list)-1:
                    print("Error: Item is already at bottom of list.") 
                    time.sleep(2)
                else:
                    PandR_list.append(workingItem)
                    PandR_list.pop(itemIndex)
                    print("Item was moved to bottom of list.")
                    time.sleep(2)
                    break
                
def swap_items(PandR_list):
    print("\n\n[Swap two items]")
    workingItem1 = input("Enter first item to swap: ")
    workingItem2 = input("Enter second item to swap: ")
    if workingItem1 and workingItem2 not in PandR_list:
        print("Error: Items not in list.")
    else:
        item1index = PandR_list.index(workingItem1)
        item2index = PandR_list.index(workingItem2)
        PandR_list[item1index], PandR_list[item2index] = PandR_list[item2index], PandR_list[item1index]
        print("Items swapped.")
        time.sleep(2)
        
def list_items(PandR_list):
    for existingItem in range(len(PandR_list)):
        print(PandR_list[existingItem])
    time.sleep(2)
    
def show_menu():
    print('''
List Maintainer 3000
Menu:    [A]dd item to list,    [R]emove item from list,
         [U] Move item up,      [D] Move item down,
         [T] Move item to top,  [B] Move item to bottom,
         [L]ist all items,      [Q]uit program,
         [S]wap two items
         ''')
 
def read_option():
    option = input("Enter an option: ").upper()
    while option not in "ARUDTBLSQ":
        print("Error: Invalid option.")
        option = input("Enter an option: ").upper()
    return option       
        
def main():
    PandR_list = ["andy", "april", "donna", "tom", "leslie"]
    
    show_menu()
    option = read_option()
    while option != 'Q':
        
        if option == 'A':
            add_item(PandR_list)
                        
        elif option == 'R':
            remove_item(PandR_list)
                      
        elif option == 'U':
            move_item_up(PandR_list)
            
        elif option == 'D':
            move_item_down(PandR_list)            
            
        elif option == 'T':
            move_item_top(PandR_list)            
            
        elif option == 'B':
            move_item_bottom(PandR_list)             
            
        elif option == 'L':
            list_items(PandR_list)         
            
        elif option == 'S':
            swap_items(PandR_list) 
                  
        show_menu()
        option = read_option()
        
    print("Bye!")
    sys.exit()

main()     
Scroll to Top