jeudi 24 janvier 2019

Dynamic if statement based on user input

I have a list with a couple of keys and values in it, each with their own keywords stored in it.

To only return specific items in the list, I use the following for loop with an if statement:

reduced_docs = []
for arg in reduced_docs:
    if 'amazon' in arg[1] and 'google' in arg[1]:
        print(arg[0])

The code above will return me all the items in the list that contain the words amazon and google. This works, but as you can see it's hardcoded now and I would something like this:

 reduced_docs = []
    keyword = input('enter your keywords:') keyword1 keyword2
     for arg in reduced_docs:
            if keyword in arg[1] and keyword in arg[1]:
                print(arg[0])

As you can see in the example code above, it's not hardcoded anymore, but what if I have 3 keywords or 4 or 5? What would I need to do to create a dynamic if statement that adds the andcondition based on the length on the input? So far I can't get my head around this.

Aucun commentaire:

Enregistrer un commentaire