Looking for items in dict

tom

I'm having an issue with dict and list: I have a function that ask user for an input, can be multiple items. The function allows only items that I have in a dict Once I get the input, I need to look for the value of each key (item) I'm trying to extract each element of the list and have multiple variables (I tried with tuple) but when I try to look for the value of each key with .get() the output is none

def main():
    i = tuple(user_item())
        *#use tuple to avoid TypeError unhashable list*
    print(i)
        *#I use the print statement here to help debugging
        #the output is: ('Name_item', )
        #which means that it founds the item in my user_item function*
    print(items.get(i))
        *#output is none, also tried:* print(items.get("i"))  = output None

def user_item():
    order = []
    while True:
        try:
            i = input("Item: ").title()
            if i in items:
                order.append(i)
                continue
        except EOFError:
            return order
main()
Luca Anzalone

The problem is that the variable i in main() is actually a tuple not a string. So, when you do items.get(i) it returns None as the dict has no tuple as keys but strings! Try instead:

for key in i:
  print(items.get(key))

Also there is no need to do i = tuple(user_item()): since user_item() returns a list, you can just have i = user_item(). This with the previous code should fix also the error "TypeError unhashable list".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related