0

im having trouble with the function bellow where i need to check if the 'key' input is already in the function parameter. only if the 'key' is already in the parameter that is passed my else should be returned. but it happens that the else is beeing returned everytime, and if the 'key' is already in the function parameter my else is returned 2 time.

def change_data(aux):
    key = input('Enter the name of the item you want to change the registration:')
    for i in aux:
        if i['Name'] == key:
            value= input('Enter the information for the item you want to change:')  
            new_value = input('Enter the new data:')
            if key == i['Name'] and (value == 'Name' or value == 'name'):
                i['Name'] = new_value
            elif key == i['Name'] and (value == 'Quantity' or value == 'quantity'):
                i['Quantity'] = float(new_value)
            elif chave == i['Name'] and (value == 'Cost' or value == 'cost'):
                i['Cost'] = float(new_value)
            elif chave == i['Name'] and (value == 'Sell' or value == 'sell'):
                i['Sell'] = float(new_value)
        else:
            print('Item not found.')

I've tryed implementing new functions and even doing a new function just to check if the key was already in the parameter, but didn'g got any luck.

  • The `else` runs for every item in the list that doesn't match the key, even though some other list items may match. – Barmar Mar 25 '22 at 19:55
  • You have to provide the line where you call the function `change_data`. We can't guess what's the value of `aux`. Please read this: https://stackoverflow.com/help/minimal-reproducible-example. – Nilton Moura Mar 25 '22 at 19:55

0 Answers0