[SOLVED] How am I to define the following functions?

Issue

I’ve a list of documents:

documents = [
        {"type": "passport", "number": "2207 876234", "name": "John Smith"},
        {"type": "invoice", "number": "11-2", "name": "James Smith"},
        {"type": "insurance", "number": "10006", "name": "Julia Smith"}
      ]

and a dictionary of directories (shelves):

directories = {
‘1’: [‘2207 876234’, ‘11-2’],
‘2’: [‘10006’],
‘3’: []
}

What I’m trying to achieve is to define functions for:

1 – adding new shelf (example conditions below)

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 – deleting the existing shelf, but only if it’s empty (example conditions below)

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

Here’s the code I’ve already written (please see below). Please help me write code for ‘#def ads’ and ‘#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.')```

Solution

The add a shelf function:

def ads(value):

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

To delete the given shelf if it is empty:

def ds(value):

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

        directories.pop(str(value), None) # deletes shelf 

Answered By – gerda die gandalfziege

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *