带有来自 txt 文件的菜单和子菜单的 Python 字典

问题描述 投票:0回答:1

我正在尝试编写一个菜单和 (2) 个子菜单,它们使用字典中的数据,由 txt 文件导入。

  1. 我认为菜单还可以,但我被困在子菜单1的方法中;
  2. 要写submenu2,是不是和submenu1一样的做法?

我从 txt 文件导入词典并创建了一个简单的菜单来显示词典、菜单、子菜单并完成应用程序。

代码:

#################### 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: ")
        #
python dictionary menu submenu
1个回答
0
投票

我对您导入的字典的格式有点困惑,但假设:

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

您应该能够在 while 循环中调用蔬菜子菜单;也就是说,在调用它之前定义 submenu_vegetable() 之后。此外,您还需要访问存储为键“蔬菜”值的蔬菜列表,如下所示:

 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!")

    •••

应该打印以下内容:

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.
© www.soinside.com 2019 - 2024. All rights reserved.