[英]How can I Query an item in the list inside the dictionary in the program?
def Program():
Storage = {}
while True:
print('1 - Query parts\n2 - Add part\n3 - Delete part\n4 - Exit\n')
choice = int(input())
if choice == 1:
while True:
print(
'1 - Consult All Parts\n2 - Consult Parts by Code\n3 - Consult Parts by Manufacturer\n4 - Return')
choice2 = int(input())
if choice2 == 1:
for items in Storage.keys():
print(f'Code: {items} \n{Storage[items]}')
elif choice2 == 2:
search = input('Enter search: ')
print(Storage[search])
elif choice2 == 3:
search = str(input('Enter the search: '))
print(Storage[search])
elif choice2 == 4:
break
elif choose == 2:
Storage[input('Code: ')] = {'Name': input('Enter item name: '), 'brand': input('Enter item brand: '), 'value(R$) ': input('Enter the value of the item: ')}
elif choose == 3:
Storage.remove[input('Choose a part to remove: ')]
elif choose == 4:
break
程序()
我需要通过制造商和代码执行这部分查询,我对python比较陌生,几天来我一直在尝试几种替代方案,但到目前为止我还没有任何进展
一个更简单的方法是集成一个list
并用每个dictionary
填充它,现在在咨询时,您会浏览列表并询问代码和制造商。
代码是这样的:
def Program():
Storage = {}
List = []
while True:
print('1 - Query parts\n2 - Add part\n3 - Delete part\n4 - Exit\n')
choice = int(input())
if choice == 1:
while True:
print(
'1 - Consult All Parts\n2 - Consult Parts by Code\n3 - Consult Parts by Manufacturer\n4 - Return')
choice2 = int(input())
if choice2 == 1:
for item in List:
print(item)
elif choice2 == 2:
search = input('Enter search: ')
for item in List:
if item.get("Code") == search: print(item)
elif choice2 == 3:
search = input('Enter the search: ')
for item in List:
if item.get("brand") == search: print(item)
elif choice2 == 4:
break
elif choice == 2:
Storage = {'Code': input('Code: '), 'Name': input('Enter item name: '), 'brand': input('Enter item brand: '), 'value(R$) ': input('Enter the value of the item: ')}
List.append(Storage)
elif choice == 3:
for item in List:
item.remove[input('Choose a part to remove: ')]
elif choice == 4:
break
Program()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.