我有一份文件清单:
documents = [
{"type": "passport", "number": "2207 876234", "name": "John Smith"},
{"type": "invoice", "number": "11-2", "name": "James Smith"},
{"type": "insurance", "number": "10006", "name": "Julia Smith"}
]和一本目录字典(书架):
directories = {
‘1’: [‘2207 876234’, ‘11-2’],
‘2’: [‘10006’],
‘3’: []
}我想要实现的是定义以下功能:
1-增加新的架子(下面的示例条件)
Please type in the command: ads
Type in the shelf number: 10
Result: Shelf has been added. Current list of shelves: 1, 2, 3, 10
Please type in the command: ads
Type in the shelf number: 1
Result: This shelf already exists. Current list of shelves: 1, 2, 32-删除现有的货架,但前提是它是空的(例如下面的条件)
Please type in the command: ds
Type in the shelf number: 3
Result: Shelf has been deleted. Current list of shelves: 1, 2
Please type in the command: ds
Type in the shelf number: 1
Result: This shelf has data. Please delete the data before deleting the shelf. Current list of shelves: 1, 2, 3这是我已经写好的代码(请看下面)。请帮助我为'#def广告‘和'#def ds’编写代码。
def people(numbers):
for doc_numbers in documents:
if doc_numbers["number"] == numbers:
print(doc_numbers["name"])
break
else:
print('This document has not been found in the database.')
def people_list():
for persons in documents:
print(persons['type'], '"'+persons['number']+'"', '"'+persons['name']+'"')
def shelf(numbers):
break_marker = False
for shelf_directories in directories.items():
for doc_numbers in shelf_directories[1]:
if doc_numbers == numbers:
print('This document is being stored on the shelf: ', shelf_directories[0])
break_marker = True
break
if break_marker == True:
break
else:
print('This document has not been found in the database.')
#def ads
#def ds
while True:
command = input('\n \
Please enter one of the commands: p, l, s, ads, ds. \n \
Type q to exit. \n \
Type help to see additional information. \n \
Your command: ')
if command == 'p':
people(input('\nPlease enter the document number:'))
elif command == 'l':
people_list()
elif command == 's':
shelf(input('\nPlease enter the document number:'))
elif command == 'ads':
ads(input('\nPlease enter the shelf number:'))
elif command == 'ds':
ds(input('\nPlease enter the shelf number:'))
elif command == 'q':
break
elif command == 'help':
print('\n \
p – people – command which will require the document number input and will put out the respective person's name;\n \
l – list – command which will put out the list of all the documents;\n \
s – shelf – command which will require the document number input and will put out the respective shelf number;\n \
ads – add shelf – command to add a new shelf;\n \
ds - delete shelf - command to delete the existing shelf)
else:
print('You've entered the wrong command. Please try again.')```发布于 2022-01-25 02:05:03
增加一个货架功能:
def ads(value):
directories[str(value)] = [] # adds empty shelf to a directories如果给定的架子是空的,则删除它:
def ds(value):
if len(directories[str(value)]) == 0: # checks if empty
directories.pop(str(value), None) # deletes shelf https://stackoverflow.com/questions/70842564
复制相似问题