Alienbot -

Hello,

I am working on the Alienbot project:
https://www.codecademy.com/paths/build-chatbots-with-python/tracks/rule-based-chatbots/modules/rule-based-chatbots/projects/python-chatbot-alienbot

Unfortunately, I have an issue with step 20/21 of this project and I simple cannot find my mistake.
In a nutshell, I need to create a method for this Alienbot class selecting the corresponding intent when a match is found. In this specific case, it should evaluate to Inside .answer_why_intent()”.

My method looks like:

def match_reply(self, reply):
    for intent, values in self.alienbabble.items():
      for regex_pattern in values:
        found_match = re.match(regex_pattern, reply.lower())
        if found_match and intent == 'describe_planet_intent':
          return self.describe_planet_intent()
        elif found_match and intent == 'answer_why_intent':
          return self.answer_why_intent()

The dictionary self.alienbabble looks like: {'describe_planet_intent': r'.*\s*your planet.*', 'answer_why_intent': r'why.*', 'cubed_intent': r'' }

If I test the method though, it does only evaluate to one the following two responses:

def describe_planet_intent(self):
    responses = ("My planet is a utopia of diverse organisms and species.","I am from Opidipus, the capital of the Wayward Galaxies. ")
    return random.choice(responses)

For example:

$ python3 script.py
Hello, may I know your name please ? Peter.
Hi Peter, I'm Etcetera. I'm not from this planet. Will you help me learn about your planet? yes
Why are you here? why
My planet is a utopia of diverse organisms and species.

I have played around with the regex patterns, but still I am unable to match the intent given a match is found. Is there maybe a mistake in the method itself ?

Thanks.

Have you tried printing out what your nested loop values are? It’s always easy to let bugs creep into those.

3 Likes

I think it might be worth considering the benefit of this nested loop structure for this problem. You have a dictionary with three terms so could you perhaps design this without loops or with just a single loop?

As a possible hint it’s worth nothing that if you iterate through a string then the character are returned element by element…

for x in 'test':
    print(x)
Out: t
e
s
t
2 Likes

Thank you for your help. Of course, I do not need a second loop given that there is only one value and a not a list of values in the dictionary. The print functionality has helped me indeed. Thanks !

1 Like