0

this is a pretty basic question but here goes:

I would like to create an array and then would like to compare a user input to those elements within the array.

If one element is matched then function 1 would run. If two elements were matched in the array the an alternative function would run and so on and so forth.

Currently i can only use an IF statement with no links to the array as follows:

def stroganoff():
    print ("You have chosen beef stroganoff")
    return

def beef_and_ale_pie():
    print ("You have chosen a beef and ale pie")
    return

def beef_burger():
    print ("You have chosen a beef burger")
    return

ingredients = ['beef','mushrooms','ale','onions','steak','burger']

beef = input("Please enter your preferred ingredients ")

if "beef" in beef and "mushrooms" in beef:
    stroganoff()
elif "beef" in beef and "ale" in beef:
    beef_and_ale_pie()
elif "beef" in beef and "burger" in beef:
    beef_burger()

As said, this is basic stuff for some of you but thank you for looking!

2
  • 1
    By array do you mean array or list? As in python both are similar but different things. If you want list, you already have one ingredients = ['beef','mushrooms','ale','onions','steak','burger']. Please can you be little clear about your intentions here. Commented Oct 7, 2016 at 23:25
  • 1
    When i say array - i actually mean list! But I can't access the list to do what I want it to as described. It works as it is but I would like to be able to add to the list and have efficient coding to compare input with list and when two words are matched then run a particular function without having to have the long ELIF statement as it currently is . Thanks! Commented Oct 7, 2016 at 23:38

3 Answers 3

1

Since you only can work with IF statements

beef=input().split()
#this splits all the characters when they're space separated
#and makes a list of them

you can use your "beef" in beef and "mushrooms" in beef and it should run as you expected it to

Sign up to request clarification or add additional context in comments.

Comments

0

So I understand your question such that you want to know, how many of your ingredients are entered by the user:

ingredients = {'beef','mushrooms','ale','onions','steak','burger'}

# assume for now the inputs are whitespace-separated:
choices = input("Please enter your preferred ingredients ").split()

num_matches = len(ingredients.intersection(choices))
print('You chose', num_matches, 'of our special ingredients.')

Comments

0

You may do something like:

# Dictionary to map function to execute with count of matching words
check_func = {
    0: func_1,
    1: func_2,
    2: func_3,    
} 

ingredients = ['beef','mushrooms','ale','onions','steak','burger']
user_input = input()

# Convert user input string to list of words
user_input_list = user_input.split()

# Check for the count of matching keywords
count = 0
for item in user_input_list:
    if item in ingredients:
        count += 1

# call the function from above dict based on the count
check_func[count]()

6 Comments

Thank you however when I run this i get the following error message: Traceback (most recent call last): File "C:/Users/Dan/Documents/Python/lists2.py", line 3, in <module> 0: func_1, NameError: name 'func_1' is not defined
What is the error message? And I believe that you have already defined func_1 , func_2 functions in your code or would have replaced these by your own functions in the dict :)
Exactly as what I thought. I just gave you the sample code. You have to replace these with your stroganoff, beef_and_ale_pie or any other function you want. Note: function should be mentioned there without ()
Hi again, it counts the number of elements but is there a way of either identifying the actual elements from the list - the key words, and/or automatically run the function. It currently only runs the beef_burger regardless of input. for example; the user inputs beef and ale, the program says: we have identified the key words beef and ale; then the program runs the beef_and_ale_pie function? Your help is much appreciated, thank you!
I've figured out how to identify the keywords found but still considering how to run function automatically!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.