0

i want the bot to Look if "Hello", "hello" or "Hi" is in the input content. I already tried with

hello = ["Hello", "hello", "Hi"]
msg = input()
if hello in msg:

and

hello = ["Hello", "hello", "Hi"]
msg = input()
if hello in str(msg):
4
  • if 'hello' in msg: Without quotes, it is considering hello as a variable and trying to fetch its value. If it is not defined then python crashes Commented Aug 18, 2021 at 16:26
  • 1
    What happened differently than what you expected when you tried those? Any errors? Commented Aug 18, 2021 at 16:27
  • 1
    Sorry i forgot to write, hello is a list means hello = ["Hello", "hello", "Hi"] Commented Aug 18, 2021 at 16:29
  • Please edit the question and include the list Commented Aug 18, 2021 at 16:29

4 Answers 4

1

If you want to check multiple values, use a list and any()

look_for = ["Hello", "hello", "Hi"]
txt = input(">> ")

if any(x in txt for x in look_for):
    print('found')
else:
    print('nope')

strings do not contain lists, so list in str will fail

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

1 Comment

Make sure you explain what is a List comprehension....
0

It looks as if you have missed quotations, all strings in python are enclosed in quotations. In your case python reads hello as a variable and won't find it. try this

if "hello" in msg:

Comments

0

Please try this:

hello=["x", "y", "z"]
msg = input(">> ")
for h in hello:
  if h in msg:
    print(msg)
  else:
    print("not in msg")

Requirements

  • you have a list of strings
  • you want to check for each in a input string
  • you may not know how to test or when to use a list comprehension

I use this basic code to check for keywords in data sets. its an easy way to test to see if I can find the data im looking for in files.

1 Comment

First of all, those are not python quotes. Next, hello and Hello are different. And the loop will keep printing msg everytime the condition is True
0

Try This :

import re

hello = ["Hello", "hello", "Hi"]
msg = "Hi; this is my world"

for i in hello:
    if i in msg:
        #your condition
    else:
        continue # else statement will run 3 times since hello has three elements

2 Comments

You don't need to split the text. Looping over the list, then using i in msg will work fine for checking substrings
yes, I don't know why I made it complicated. Thank you

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.