Home > Software design >  Can't compare the whole set
Can't compare the whole set

Time:02-01

I was trying to take multiple input at once by doing this from the user

user_ask = input("Your desired ingredients: ").split(', ' or ' , ')

and compare it with the another list:

menu = ["Mushroom" , "Bacon" , "Sausage" , "Pepperoni" , "Chicken"]

Suppose, the user inputted:

"Mushroom" and "Bacon"

now, as the user has inputted, the user_ask variable is list.

What I want is, to compare the lists: (user_ask & menu)

If all the elements in the list(user_ask) matches with the list(menu) I wanna proceed.

I did this...

user_ask = input("Your desired ingredients: ").split(', ' or ' , ')

x = []
print("\nYou have chosen: ")
for order in user_ask:
    m = order.title()
    x.append(m)
    print(m)
x_set = set(x)

if (x_set & menu_set):
    if "Mushroom" in x:
        print("Adding Mushroom...")
        for waiting_time in range(3,0,-1):
            time.sleep(1)
            print(waiting_time)
        time.sleep(0.7)
        print("Mushroom added!")

If I do this, then when the user types "Mushroom" and then "Something out of the menu", the code proceeds with only mushroom and not prints out that the second element does not matches with the menu.

What I want is.... First of all scan all the value in the user_input and match with the menu, if any element doesn't gets matched with the menu then quit the program

CodePudding user response:

user_ask = input("Your desired ingredients: ").split(', ' or ' , ')
menu = ["Mushroom" , "Bacon" , "Sausage" , "Pepperoni" , "Chicken"]
x = []
print("\nYou have chosen: ")
for order in user_ask:
    m = order.title()
    x.append(m)
    print(m)
x_set = set(x)
menu_set = set(menu)

for ingredient in x_set :
    if ingredient in menu_set :
        print("Adding {}...".format(ingredient))
        for waiting_time in range(3,0,-1):
            time.sleep(1)
            print(waiting_time)
        time.sleep(0.7)
        print("{} added!".format(ingredient))
    else :
        print("{} not in menu".format(ingredient))
        break

Output 1 when all the elements of user_ask were in menu :-

Your desired ingredients: mushroom, bacon

You have chosen: 
Mushroom
Bacon
Adding Mushroom...
3
2
1
Mushroom added!
Adding Bacon...
3
2
1
Bacon added!

Output 2 when all the elements of user_ask were not in menu :-

Your desired ingredients: mushroom, beef

You have chosen: 
Mushroom
Beef
Adding Mushroom...
3
2
1
Mushroom added!
Beef not in menu

CodePudding user response:

Here is basic logic for traversing data, append is not included. Hope this helps

menu = [
    "Mushroom",
    "Bacon",
    "Sausage",
    "Pepperoni",
    "Chicken"
]

item_found = False

user_ask = input("Your desired ingredients (saperated by comma ,) : ")

user_menu = user_ask.split(',')

for item in user_menu:
    striped_item_name = item.strip()
    for menu_item in menu:
        if menu_item == striped_item_name:
            #here goes logic for matched items
            item_found = True
            print("Item found : ", striped_item_name)
            continue
            
if item_found == False:
    #here goes logic for unmatched items
    print("element doesn't gets matched with the menu")
        
  • Related