2

My error appears on this line:

if exclude3 not in Sent:

And it is:

TypeError: 'in <string>' requires string as left operand, not set

My code is:

import string

Word = input("Please give a word from the sentence")

exclude3 = set(string.ascii_letters)

if exclude3 not in Sent:
    print("")
elif exclude3 not in Word:
    print("")
else:

What is a left operand? What am I doing wrong and is there a simpler way to accomplish what I want? Should I be using something other than in and what should that be?

Idos
  • 15,053
  • 14
  • 60
  • 75
Out North
  • 43
  • 5
  • 1
    what do you hope to achieve with this code? your error is self explanatory but what you are trying to do is not – SirParselot Feb 02 '16 at 13:33

3 Answers3

1

exclude3 is not a string it is a set.
You try to use the in operator to check if a set is contained in another set which is wrong.

Maybe you meant to write: if Sent not in exclude3 ?

Idos
  • 15,053
  • 14
  • 60
  • 75
0

You need to check if the set and the string overlap. Either

if not exclude3.intersection(Sent):

or

if not any(x in Sent for x in exclude3):

will have the desired result.

The in operator works by testing if the left-hand argument is an element of the right-hand argument. The exception is str1 in str2, which tests if the left-hand str is a substring of the other.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

When you use in operand the left side and right side objects must be same type. And in this case exclude3 is a set object and you can not check its membership in a string.

Example:

>>> [] in ''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not list

If you want to check the existence of all the items of a set in a string you can use set.intersection() as following :

if exclude3.interection(Sent) == exclude3:
      # do stuff

And for any intersection just check the validation of exclude3.interection(Sent):

if exclude3.interection(Sent):
     # do stuff
Mazdak
  • 105,000
  • 18
  • 159
  • 188