-5

[What I've tried] (https://i.stack.imgur.com/N8ME6.png)

So I know this question has been asked (and answered) before, but how can I make a variable search for one or more specific keywords within a user response? For context, I want to make variable "mla9url" look at the response and check to see if exactly 1 word from each list (being keywords1 and keywords2), and if mla9url finds that there ARE 1 from each list, then the output will say "valid", else it will output "invalid".

p.s. I kinda new to python, not totally sorted out with these cmds and functions.

Tried: To get a variable to recognize a certain string in a given response (couldn't get it to work).

Expected: To get a variable to recognize a certain string in a givin response.

  • 6
    [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – Axe319 Mar 08 '23 at 18:02
  • you have an error in your `and` operator. This is not how to use them. Check in your favorite python tutorial how to apply logical operators. https://realpython.com/python-and-operator/ – Sembei Norimaki Mar 08 '23 at 18:14
  • Does this answer your question? [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al). While this question refers to the `or` operator, the root cause of the problem is similar to yours. – Ignatius Reilly Mar 08 '23 at 18:16
  • _I know this question has been asked (and answered) before_. Why the other answers don't work for you? – Ignatius Reilly Mar 08 '23 at 18:20
  • Try `if keywords2: print('valid')`. This may give you a clue on what's happening. – Ignatius Reilly Mar 08 '23 at 18:28

1 Answers1

0
keywords1 = ['2', 'keya', 'keywords', 'etcd']
keywords2 = ['1', 'keys', 'keyword', 'etc']

mla9url = str(input('Enter a user response: '))

valid = False
x, y = 0, 0
for a in range(len(keywords1)):
    if keywords1[a] in mla9url:
        x += 1
        if x > 1:
            break
for b in range(len(keywords2):
    if keyboards2[b] in mla9url:
        y += 1
        if x > 1 or y > 1:
            break

if(x = 1 and y = 1):
    valid = True
else:
    valid = False
Jonathan
  • 185
  • 9
  • Sorry it's not the most efficient possible, I don't have time to go deep into depth on this. It would help if you added an example of a user generated input. I might improve my answer when I have time... – Jonathan Mar 08 '23 at 18:38