0

How can I make Python quit if a list contains a particular word?

text = input("Input text: ")
if["exit","quit","close"] in text:
    Quit()
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38

2 Answers2

3

It's the other way around

if text in ["exit","quit","close"]:

You can read it as pseudo English, you are checking if the string is in the list.

If you want to check if part of the input is in the list you can use any

if any(x in ["exit", "quit", "close"] for x in text.split(' ')):
Guy
  • 46,488
  • 10
  • 44
  • 88
1

You've got it backwards. Try

if text in ["...","..."]:

Think of it as "If this is in that".

stolenmoment
  • 443
  • 3
  • 6