dimanche 13 décembre 2020

getting if text is equal to another with wildcard

I don't know if it was understandable by the title, but what I want is this: I have a list with some input and output for that input. If the user enters the input, a response is given.

import random
answer_list = [
  [
    "some*text",
    ["ran", "dom"]
  ]
]
replace = '*'

def get_response(text):
 ANSWER = False
 if self.replace in text.lower():
   for elem in self.answer_list:
                    
     answer = elem[0].split(self.replace)

     answer_found = True

     for content in answer:
       if content in text.lower():
         continue
       else:
         answer_found = False
         break

       if answer_found == True:
         last = 0

         for element in answer:
           if answer.find(element) > last:
             last = answer.find(element)
             continue
           else:
             answer_found = False
             break
                        
         if answer_found == True:
           return random.choice(elem[1])

         else:
           continue
  if ANSWER == False:
    return 'None'

What this should do is use the * as a 'wildcard' (i don't know how to call it) and get the answer. for example: some__text returns ran or dom but its returning 'None' any ideas? (the def is inside a class, thats why the self)




Aucun commentaire:

Enregistrer un commentaire