Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Dialogues by alterGNU
class Chat:
def __init__(self):
self.H_dialogue=""
self.R_dialogue=""
def add_dialogues(self, H_mess, R_mess):
if len(self.H_dialogue): # or len(self.R_dialogue)
self.H_dialogue+="\n"+str(H_mess)
self.R_dialogue+="\n"+str(R_mess)
else:
self.H_dialogue+=H_mess
self.R_dialogue+=R_mess
def connect_human(self, human):
human.Chat=self
def connect_robot(self, robot):
robot.Chat=self
def show_human_dialogue(self):
return self.H_dialogue
def show_robot_dialogue(self):
return self.R_dialogue
class Chatter:
def __init__(self, name):
self.name=name
self.Chat=None
def send(self, message):
new_message="".join([ "0" if e.lower() in "aeiou" else "1" for e in message ])
self.Chat.add_dialogues(f"{self.name} said: {message}",f"{self.name} said: {new_message}")
class Human(Chatter):
def __init__(self, name):
super().__init__(name)
class Robot(Chatter):
def __init__(self, name):
super().__init__(name)
# OR:
# Human = Robot = Chatter
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
chat = Chat()
karl = Human("Karl")
bot = Robot("R2D2")
chat.connect_human(karl)
chat.connect_robot(bot)
karl.send("Hi! What's new?")
bot.send("Hello, human. Could we speak later about it?")
assert chat.show_human_dialogue() == """Karl said: Hi! What's new?
R2D2 said: Hello, human. Could we speak later about it?"""
assert chat.show_robot_dialogue() == """Karl said: 101111011111011
R2D2 said: 10110111010111100111101110011101011010011011"""
print("Coding complete? Let's try tests!")
June 4, 2021