0

the if "something" in r or "something else" in r seems a bit repetitive, is there a better or more efficient way to do this?

aiBot = Cleverbot()
r = aiBot.send(question).lower()
ff = ""
if "yes" in r or "of course" in r or "absolutely" in r:
    ff = "yes"
elif "no" in r or "i don't think so" in r:
    ff = "no"
haze
  • 1

1 Answers1

1

Not exactly sure if this is the best way to do it but at least if your list starts increasing it should work. Then again a bot shouldn't really be made like that.

accept_list = ["yes","of course", "you get my point"]
reject_list = ["no", "i don't think so"]

aiBot = Cleverbot()
r = aiBot.send(question).lower()
ff = ""
if any(ans in r for ans in accept_list):
    ff = "yes"
elif any(ans in r for ans in reject_list):
    ff = "no"

Suddenly thought about another way though probably still not the best

using_dict = {
  "yes":"yes",
  "of course": "yes",
  "no":"no",
  "i don't think so": "no"
}

r = "nope"
try:
  ff = using_dict[r]
except:
  ff="default value"
Solly
  • 41
  • 5