pythondictionarymenusubmenu

Python dictionary with Menu & Submenu from a txt file


I'm trying to write a menu and (2) submenu(s) that use data from a dictionary, imported by a txt file.

  1. I think the menu is ok but I'm stuck in the approach to the submenu1;
  2. To write the submenu2, will it be the same approach to submenu1?

I imported the dictionary from a txt file and created a simple menu to Show the Dictionary, Menu, Submenus and finish the app.

CODE:

#################### IMPORT TXT FILE AND READ AS DICTIONARY 
basket = {"vegetable":[], "fruit":[]}
options = list(basket.keys())
fp = open("fruits_vegetables.txt", "r", encoding="UTF-8-SIG")
cont = fp.readlines() 
for linha in cont:
    dados = linha.split(",")
    dici[dados[0]].append(dados[1][0:-1])


###########################################
def menu ():
    print ("\nM E N U")
    print ("a. Show the basket")
    print ("b. Clean app")
    print ("c. Vegetable")
    print ("d. Fruit")
    print ("e. End app")

menu()
option = input("Select an option ")

while option != "e":
    
    if option == "a":
        for item in basket:
            print(item,":",basket[item])
            
    elif option == "b":
        for x in range(15):
          print("\n")
          
    elif option == "c":
        #Enter submenu???? HOW????
                
    elif option == "d":
        #Entrar submenu fruit??? HOW???
        
    else:
        print("Select a valid option!")

    print()
    menu()
    option = input("Select an option ")

print("Thanks for using this app.")


###############################################

def submenu_vegetable ():
    print ("\nV E G E T A B L E S")
    print ("1. List vegetables")
    print ("2. Remove vegetable")
    print ("3. Add vegetable")
    print ("4. Edit name of vegetable")

option_vegetable = input("Select an option: ")

    if option_vegetable == "1":
        print #??? keys?
    elif opcao_legume == "2":
        item = input("Select a vegetable to remove: ")
        del(basket[item])
    elif option_vegetable == "3":
        #add vegetable?? = input(("Select a vegetable to add: ")
        #

Solution

  • I'm a little confused on exactly how your imported dictionary is formatted but assuming:

    basket: dict = {'vegetable': [], 'fruit': []}
    

    you should be able to call the vegetable sub menu within your while loop; that is, after you define submenu_vegetable() before you call it. Also, you'll need to access the list of vegetables stored as a value for the key 'vegetable' like so:

     basket = {"vegetable": ['carrot', 'spinach', 'kale', 'peas'],
               "fruit": ['mango', 'apple', 'lime', 'nectarine']}
    
    def menu():
        print("\nM E N U")
        •••
        print("e. End app")
    
    def submenu_vegetable():
        print("\nV E G E T A B L E S")
        print("1. List vegetables")
        print("2. Remove vegetable")
        print("3. Add vegetable")
        print("4. Edit name of vegetable")
    
        option_vegetable = input("Select an option: ")
    
        if option_vegetable == "1":
            for vegetable in basket['vegetable']:
                print(vegetable)
        elif option_vegetable == "2":
            item = input("Select a vegetable to remove: ")
            if item in basket['vegetable']:
                basket['vegetable'].remove(item)
                print('{i} was removed from '
                      'the vegetable basket.'.format(i=item))
        elif option_vegetable == "3":
            item = input("Select a vegetable to add: ")
            if item not in basket['vegetable']:
                basket['vegetable'].append(item)
                print('{i} was added to '
                      'the vegetable basket.'.format(i=item))
    
    def submenu_fruit():
        pass
    
    menu()
    option = input("Select an option: ")
    
    while option != "e":
        •••    
        elif option == "c":
            submenu_vegetable()
    
        elif option == "d":
            submenu_fruit()
        else:
            print("Select a valid option!")
    
        •••
    

    which should print the following:

    M E N U
    a. Show the basket
    b. Clean app
    c. Vegetable
    d. Fruit
    e. End app
    Select an option c
    
    V E G E T A B L E S
    1. List vegetables
    2. Remove vegetable
    3. Add vegetable
    4. Edit name of vegetable
    Select an option: 2
    Select a vegetable to remove: kale
    kale was removed from the vegetable basket.