首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何定义以下功能?

如何定义以下功能?
EN

Stack Overflow用户
提问于 2022-01-25 01:50:38
回答 1查看 60关注 0票数 1

我有一份文件清单:

代码语言:javascript
复制
documents = [
        {"type": "passport", "number": "2207 876234", "name": "John Smith"},
        {"type": "invoice", "number": "11-2", "name": "James Smith"},
        {"type": "insurance", "number": "10006", "name": "Julia Smith"}
      ]

和一本目录字典(书架):

代码语言:javascript
复制
directories = {
‘1’: [‘2207 876234’, ‘11-2’],
‘2’: [‘10006’],
‘3’: []
}

我想要实现的是定义以下功能:

1-增加新的架子(下面的示例条件)

代码语言:javascript
复制
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, 3

2-删除现有的货架,但前提是它是空的(例如下面的条件)

代码语言:javascript
复制
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’编写代码。

代码语言:javascript
复制
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.')```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-25 02:05:03

增加一个货架功能:

代码语言:javascript
复制
def ads(value):

   directories[str(value)] = [] # adds empty shelf to a directories

如果给定的架子是空的,则删除它:

代码语言:javascript
复制
def ds(value):

    if len(directories[str(value)]) == 0: # checks if empty 

        directories.pop(str(value), None) # deletes shelf 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70842564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档