Hello can someone help with this project this is my code and idk what i did wrong because after i write my name and say yes to help it return no_match_intent responses
# importing regex and random libraries
import re
import random
class AlienBot:
# potential negative responses
negative_responses = ("no", "nope", "nah", "naw", "not a chance", "sorry")
# keywords for exiting the conversation
exit_commands = ("quit", "pause", "exit", "goodbye", "bye", "later")
# random starter questions
random_questions = (
"Why are you here? ",
"Are there many humans like you? ",
"What do you consume for sustenance? ",
"Is there intelligent life on this planet? ",
"Does Earth have a leader? ",
"What planets have you visited? ",
"What technology do you have on this planet? "
)
def __init__(self):
self.alienbabble = {'describe_planet_intent': r'.*\s*your planet.*',
'answer_why_intent': r'why\sare.*',
'cubed_intent': r'.*cube.*(\d+)'
}
# Define .greet() below:
def greet(self):
self.name = input("Hello there, what's your name? ")
will_help = input(f"Hi {self.name}, I'm Etcetera. I'm not from this planet. Will you help me learn about your planet? ")
if will_help in self.negative_responses:
print("Ok, have a nice Earth day!")
return
self.chat()
# Define .make_exit() here:
def make_exit(self, reply):
for word in self.exit_commands:
if word in reply:
print("Ok, have a nice Earth day!")
return True
# Define .chat() next:
def chat(self):
reply = input(random.choice(self.random_questions)).lower()
while not self.make_exit(reply):
reply = input(self.match_reply(reply))
# Define .match_reply() below:
def match_reply(self, reply):
for key, value in self.alienbabble.items():
intent = key
regex_pattern = value
found_match = re.match(regex_pattern, reply)
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()
elif found_match and intent == "cubed_intent":
return self.cubed_intent(found_match.groups()[0])
else:
return self.no_match_intent()
# Define .describe_planet_intent():
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. ", "I am from Mars "]
return random.choice(responses)
# Define .answer_why_intent():
def answer_why_intent(self):
responses = ["I come in peace", "I am here to collect data on your planet and its inhabitants. ", "I heard the coffee is good. "]
return random.choice(responses)
# Define .cubed_intent():
def cubed_intent(self, number):
number = int(number)
cubed_number = number ** 3
return f"The cube of {number} is {cubed_number}. Isn't that cool? "
# Define .no_match_intent():
def no_match_intent(self):
responses = ["Please tell me more. ", "Tell me more! ", "Why do you say that? ", "I see. Can you elaborate? ", "Interesting. Can you tell me more? ", "I see. How do you think? ", "Why? ", "How do you think I feel when you say that? "]
return random.choice(responses)
# Create an instance of AlienBot below:
my_bot = AlienBot()
my_bot.greet()`Preformatted text`
Without formatting this is very hard to debug, please see How do I format code in my posts? for posting code to the forums.
If that function is getting called more often than you expect I’d highly suggest debugging the logic statements which lead to it being run. So get some print statements or something in your match_reply
function to make sure your tests can ever actually evaluate to True
(have a look at the values you’re testing and the output of those logical expressions).
hi i just format it can help now please
Have you followed any of the advice previously given?
i dont know to much about how to debug can you be more explicit please
where and what should i print
Learning to debug is part of learning to program, have a think about what set of circumstances could lead to the function you don’t want to call getting called. Try poking around that area.
ok sorry cuz maybe i look dumb i try now and i come to see if i can do something
That’s the best option, it’s very much a learn by doing exercise that kind of debugging. You expect a value to be x
the computer has instead stored 0xf3k13400
. You need to find out if and when something like this has happened. Work logically backwards from the bit where you know it’s not doing what it’s supposed to.
oh idk what is wrong i see after some testing only describe_planet_intent is working and rest are going to no_match_intent
and this code is working perfect the promblem is where i comented
import re
import random
class AlienBot:
potential negative responses
negative_responses = (“no”, “nope”, “nah”, “naw”, “not a chance”, “sorry”)
keywords for exiting the conversation
exit_commands = (“quit”, “pause”, “exit”, “goodbye”, “bye”, “later”)
random starter questions
random_questions = (
"Why are you here? ",
"Are there many humans like you? ",
"What do you consume for sustenance? ",
"Is there intelligent life on this planet? ",
"Does Earth have a leader? ",
"What planets have you visited? ",
"What technology do you have on this planet? "
)
def init(self):
self.alienbabble = {'describe_planet_intent': r'.*\s*your planet.*',
'answer_why_intent': r'why\sare.*',
'cubed_intent': r'.*cube.*(\d+)'
}
Define .greet() below:
def greet(self):
self.name = input("Hello there, what's your name? ")
will_help = input(f"Hi {self.name}, I'm Etcetera. I'm not from this planet. Will you help me learn about your planet? ")
if will_help in self.negative_responses:
print("Ok, have a nice Earth day!")
return
self.chat()
Define .make_exit() here:
def make_exit(self, reply):
for word in self.exit_commands:
if word in reply:
print("Ok, have a nice Earth day!")
return True
Define .chat() next:
def chat(self):
reply = input(random.choice(self.random_questions)).lower()
while not self.make_exit(reply):
reply = input(self.match_reply(reply))
Define .match_reply() below:
def match_reply(self, reply):
for key, value in self.alienbabble.items():
intent = key
regex_pattern = value
found_match = re.match(regex_pattern, reply)
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()
elif found_match and intent == "cubed_intent":
return self.cubed_intent(found_match.groups()[0])
# if i uncoment this code now work but without this all is working
#else:
#return self.no_match_intent()
Define .describe_planet_intent():
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. ", "I am from Mars "]
return random.choice(responses)
Define .answer_why_intent():
def answer_why_intent(self):
responses = ["I come in peace ", "I am here to collect data on your planet and its inhabitants. ", "I heard the coffee is good. "]
return random.choice(responses)
Define .cubed_intent():
def cubed_intent(self, number):
number = int(number)
cubed_number = number ** 3
return f"The cube of {number} is {cubed_number}. Isn't that cool? "
Define .no_match_intent():
def no_match_intent(self):
responses = ["Please tell me more. ", "Tell me more! ", "Why do you say that? ", "I see. Can you elaborate? ", "Interesting. Can you tell me more? ", "I see. How do you think? ", "Why? ", "How do you think I feel when you say that? "]
return random.choice(responses)
Create an instance of AlienBot below:
my_bot = AlienBot()
my_bot.greet()
i think i found the problem the problem is here:
def match_reply(self, reply):
for key, value in self.alienbabble.items():
intent = key
regex_pattern = value
found_match = re.match(regex_pattern, reply)
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()
elif found_match and intent == "cubed_intent":
return self.cubed_intent(found_match.groups()[0])
Because the loop going though each value and key and if i put an else in final it exit at first
So first key ‘describe_planet_intent’ and value is ‘.\syour planet.*’ that why this work and then if i put else and i write for the ‘answer_why_intent’ it cant get there because is going tought the if first and it exit
So here is the solution :
def match_reply(self, reply):
for key, value in self.alienbabble.items():
intent = key
regex_pattern = value
found_match = re.match(regex_pattern, reply)
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()
elif found_match and intent == "cubed_intent":
return self.cubed_intent(found_match.groups()[0])
return self.no_match_intent()
The return self.no_match_intent() dont need to be in the if condition it need to be outside the for loop and after the program goes thought the loop and it dont match anything it return this.
Thanks to @tgrtim if he told me the solution from the first I don’t learn anything now after I succeeded alone I am happy and I learned and understood some things better
Fantastic. Now that you’ve done this once the next time will be easier too
yee thanks at first i was mad cuz u dont tell but i understand why and thanks u dont tell me