В моем коде я разрешаю пользователю вводить значение ключа словаря для редактирования. Я хотел бы сделать так, чтобы пользователь мог вводить только те значения c, чтобы dict не добавлялся. вот мой код. требуется обработка исключений при редактировании и значении переменных.
def edit_items(info):
xy = info
while True:
print('Index | Orders')
for x in range(len(xy)):
print(x,xy[x])
choice = int(input('Which entry would you like to edit?\nChoose by index: '))
print(xy[choice])
edit = input('What you want to edit: ') # Key val of dict
value = input("Enter: ") # Value for the specific key in dict
xy[choice][edit.capitalize()] = value
print('list updated.')
print(xy[choice])
with open('cart.txt','w') as file:
file.write(str(xy))
file.close()
more_edits = input('\nDo you want to make more edits?(y/n): ')
if more_edits == 'n':
break
print(xy)
пример вывода, который видит пользователь.
0 {'Item': 'edd', 'Price': '1.0', 'Quantity': '1'}
1 {'Item': 'milk', 'Price': '1.0', 'Quantity': '1'}