The following program let the user define the order in which the key names (elements) from the_dictionary_list will be inserted into Keys_input:
the_dictionary_list = {'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 'Cuerpo': ['Cuerpo_cangrejo.png'], 'Fondo': ['Oceano.png'], 'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'], 'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
# creating an empty list
Keys_input = []
# number of elements
n = len(the_dictionary_list)
i = 0
print('\n')
print('The following "keys" represent the name of the folders in the current path')
while True:
AllKeysNames = the_dictionary_list.keys()
print('\033[46m' str(AllKeysNames) '\033[0m')
ele = input("\033[0;37;40mNow It's time to define the order in which the Cartesian Products will be made, tell me which valid key you want me to set now:\033[0m ")
if ele in the_dictionary_list and ele not in Keys_input:
Keys_input.append(ele) # adding the element
i = 1
print(f'\033[0;37;42mThe array has been updated, its current storage is the following {Keys_input}\033[0m')
if i == n:
print("\u001b[45mThe array is now full, let's continue with the next step\033[0m")
break
else:
if ele not in the_dictionary_list:
print('\u001b[43mPlease, type only valid key names\033[0m')
if ele in Keys_input:
print('\u001b[43mStop, that key IS ALREADY SAVED in the array, try with a different valid one\033[0m')
print(f'\u001b[45mCurrent storage of the array is the following {Keys_input}\033[0m')
Now, assume that the user chooses the following order for the elements in Keys_input:
['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']
How could the program above be improved to add automatically the last element left (in this case 'Puas') to the array when there are no more key names left? I mean, right after the Keys_input array is updated as follows:
['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas']
CodePudding user response:
Figured it out. After adding the penultimate element to the array, the program will use a for loop to iterate over the dictionary, and it will evaluate which key is not in the array, the one that is not there (as it will always be the last one) will be added automatically by the program. After this, the program breaks the while True loop of the original code
if i == (n-1):
for key in the_dictionary_list:
if key not in Keys_input:
Keys_input.append(key)
print(f'\033[0;37;42mLet me add the last key, the final storage is the following {Keys_input}\033[0m')
print("\u001b[45mThe array is now full, let's continue with the next step\033[0m")
break
