Sanity checks of whisky list in my cellar with while loop with Continue keyword in Python

Let’s say that 3 years ago i looked into my cellar and made a detailed inventory of what I found there. This list is 100% reliable. This is a nested dictionary.

{ 'whisky': { '1983': 'full',
              '1981': 'full',
              '1976': 'label damaged '},
  'vodka': {'2005': 'funny smell'},
  'champagne': {'2018': 'pricey'}}

However, when I went to the cellar I realized that I had drunk the champagne a long time ago already. I prepared two simple text files with the names of what I still have. The first file is called whisky, the second file is called vodka. The files are empty.

Now I want to get rid of this item in my nested dictionary because i don’t have it anymore. The continue phrase in Except clause will help me sanitize the list and go back to the main Try clause.

import pprint
  Dict = { 'whisky': { '1983': 'full',
              '1981': 'full',
              '1976': 'label damaged '},
  'vodka': {'2005': 'funny smell'},
  'champagne': {'2018': 'pricey'}}
#i'm gathering here list of alcohol types
dictbottlelist = []
for i in Dict.keys():
  for j in Dict[i].keys():

while True:
  #trying to open file with a specific alcohol type
    x = 0
    for x in range(x,len(dictbottlelist)):
      with open(dictbottlelist[x], 'r') as reader:
  #if the file with alcohol type doesn't exist, delete it from inventory and continue with the loop      
  except FileNotFoundError as e:
      print(str(e) + '.This bottle is GONE ' + dictbottlelist[x])
      del Dict[dictbottlelist[x]]
      print('An error in the list has been found and removed. ')
    print('After removing all errors from inventory, this is what you have in your cellar. ')
    pp = pprint.PrettyPrinter(indent=4)
this is what you thought you had in your cellar:
{ 'champagne': {'2018': 'pricey'},
'vodka': {'2005': 'funny smell'},
'whisky': {'1976': 'label damaged ', '1981': 'full', '1983': 'full'}}
[Errno 2] No such file or directory: 'champagne'.This bottle is GONE champagne
An error in the list has been found and removed.
After removing all errors from inventory, this is what you really have in your cellar.
{ 'vodka': {'2005': 'funny smell'},
'whisky': {'1976': 'label damaged ', '1981': 'full', '1983': 'full'}}

I will use this to sanitize my config change file in the other blog post…


Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:


Komentujesz korzystając z konta Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Połączenie z %s

%d blogerów lubi to: