0
variableName=["display","screen","sound""audio"]
fileName=["PPP", "Abc"]
P1="PPP"
d="display"
s="screen"
ss="sound"
a="audio"
d=P1
loop=True
def CH(variableName, fileName, loop):
    while loop==True:
        Up=input("What is your problem?\n")
        if (variableName) in Up.lower():
            file = open(fileName + ".txt", "r")
            whole= file.read()
            print(whole)
            file.close()
            loop=False
        else:
            loop=True
            continue**

CH(variableName, fileName, loop)

I am trying to create a function that read your input and give an answer by using keywords. But the error ""TypeError: 'in ' requires string as left operand, not list"" keeps coming up and I can seem to fix it

1
  • What do you expect if (variableName) in Up.lower(): to do? Commented Mar 3, 2016 at 18:08

3 Answers 3

2

I think you've got the logic of your in statement backwards. You should be checking

if Up.lower() in variableName:

not the other way around.

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

Comments

0

Using

if (variableName) in Up.lower():

....tests for the existence of a tuple (variableName) in a string Up.lower().

Since variableName is actually a list of strings, depending on what you are trying to achieve, you may well want to use one of the following instead:

if any(s in Up.lower() for s in variableName):

if all(s in Up.lower() for s in variableName):
  • The any() option will return True if any of the strings s in variableName are in Up.lower().
  • The all option will return True if all of the strings s in variableName are in Up.lower().

Comments

0

If variableName is a list, you can check membership with .__contains__().

Try changing the if statement to if variableName.__contains__(Up.lower):

Comments

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.