I have tried to make a piece of code that shows me only the common values of user input as compared to a set list. I continue to get the error:
TypeError: unhashable type: 'list'
Here's what I've done:
main_List = ["name", "job", "backstory", "all"]
provided_List = []
def main()
resp = input("Do you need a name, job, backstory, or all?")
provided_List.append(resp.split())
update_Set = provided_List.intersection(main_List)
print(update_Set)
main()
Essentially, I'm trying to ignore everything but key words the user inputs. As far as I can tell I am doing everything exactly like the examples but it still isn't coming out the way I want. I'm not sure what I'm missing here.
CodePudding user response:
You are attempting to use set
methods on list
objects. You need to cast both main_list
and provided_list
as set
objects:
main_options = {"name", "job", "backstory", "all"}
prompt = "Select any of name, job, backstory, or all: "
resp = main_options & set(input(prompt).split())
Usage:
Select any of name, job, backstory, or all: name
# resp: {'name'}
Select any of name, job, backstory, or all:
# resp: set()
Select any of name, job, backstory, or all: name backstory job
# resp: {'backstory', 'job', 'name'}