I'm confused about how to compare user input and a set list on python. For this problem, I have to create a program that asks the user for a list of ingredients. As the user enters the ingredients, I then need to store them in a list. Once the user is done entering their list, I need to pass this list to a function that will compare the ingredients to an already existing list of pantry items. If all items are available in the pantry, it should print out "you don’t need to go shopping". If any item is missing, it should print out "you need to go shopping" and list the missing ingredients. I've tried several ways of doing it but I can't figure it out.
So basically my function has to have: 1: A pre-created list for pantry items 2: User input into an ingredient list 3: Pass the ingredient list to a method 4: Use a conditional and loop in the method 5: Print out the results of whether the user needs to go shopping based on the items in the ingredient list that are not in the pantry.
This is what I have right now. Whenever I run it, it allows me to type in an item but then says <function end at 0x7fec87bf58b0> :
shopping_list = [] set_list = ['bread', 'jelly', 'peanut butter', 'chips']
#prints out what I need or if I have everything on my list def end(): for item in shopping_list: if shopping_list == set_list: print("you have everything") else: print("You have to go grocery shopping! You need:" str(new_list))
while True: #asks for new items and removes them from the set list to return a new list try: new_item = input("Item? : ") except ValueError: print("Oops! That was no valid item. Try again...") new_list = set_list.remove(new_item) print(new_list)
if new_item == 'done':
print(end)
CodePudding user response:
end
is a function. When you write print(end)
you ask python to print info about function. So it prints <function end at 0x7fec87bf58b0>. To print a result of a function you should call it first
So the end of your script will look like
if new_item == 'done':
result = end() # calling a function
print(result)
CodePudding user response:
There you go:
pantry_items = ["apple", "banana", "eggs"]
ingridient_list = []
shopping_list = []
def compare_lists():
for item in pantry_items:
if item not in ingridient_list:
shopping_list.append(item)
userinput = ""
while (len(ingridient_list) < len(pantry_items)):
userinput = input("Enter smth.: ")
if (userinput == "done"):
compare_lists()
if len(shopping_list) > 0:
print("You need:")
print(shopping_list)
else:
print("You dont need to go shopping")
break
ingridient_list.append(userinput)
My python is a bit rusty, but this will do the job